Python Forum

Full Version: CSV file from Binary to String
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I try to open a CSV file from internet.
Then I try to parse it with CSV lib.
I get error that I deal with it in Binary.
any help?

with urllib.request.urlopen(url) as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                print(row.decode('utf-8'))
Output:
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
If I were in your situation, I'd either read the CSV string into memory and work with it from there, or if it's too big, then download it to disk and use a context manager on that created file.
Quote:_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
urlopen() always return bytes and csv reader need string.
Then it's to late to decode on row.
import urllib.request
import csv

url = 'adress'
site = urllib.request.urlopen(url)
with open(site.read().decode('utf-8')) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row)
Use always Requests then do not need to guess on encoding as get correct encoding from site.
import requests

url = 'adress'
site = requests.get(url)
with open(site.text) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row)
It's usually utf-8,but csv files can have other encoding.
>>> import requests
>>> 
>>> url = 'https://www.stats.govt.nz/....bands-csv.csv'
>>> site = requests.get(url)
>>> site.encoding
'ISO-8859-1' 
Also one of the simplest way out there is to use Pandas.
import pandas as pd

data = pd.read_csv('https://some.csv')