12
I'm trying to use the csv module to read a utf-8 csv file, and I have some trouble to create a generic code for python 2 and 3 due to encoding.
Here is the original code in Python 2.7:
with open(filename, 'rb') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
But when I run it with python 3, it doesn't like the fact that I open the file without "encoding". I tried this:
with codecs.open(filename, 'r', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Now python 2 can't decode the line in the "for" loop. So... how should I do it ?