The following pattern is used often to read csv files:
with open(filename, 'rU') as csvfile:
reader = unicodecsv.DictReader(csvfile)
I think this worked in python2 since the str and bytes types were synonymous. However, this breaks in python3 since unicodecsv expects the file to be opened in binary mode, which it is not.
For example, the following fails in python3 with the error AttributeError: 'str' object has no attribute 'decode'
import unicodecsv
filename = 'openelex/us/md/mappings/md.csv'
with open(filename, "r") as data:
reader = unicodecsv.DictReader(data)
for row in reader:
print(row)
Using csv instead of unicodecsv fixes the issue.
import csv
filename = 'openelex/us/md/mappings/md.csv'
with open(filename, "r") as data:
reader = csv.DictReader(data)
for row in reader:
print(row)
Is there something wrong with my setup, or is this broken for other people as well?
The following pattern is used often to read csv files:
I think this worked in python2 since the
strandbytestypes were synonymous. However, this breaks in python3 sinceunicodecsvexpects the file to be opened in binary mode, which it is not.For example, the following fails in python3 with the error
AttributeError: 'str' object has no attribute 'decode'Using
csvinstead ofunicodecsvfixes the issue.Is there something wrong with my setup, or is this broken for other people as well?