Bussiere
Bussiere

Reputation: 1164

Unicode error in Python 3 reading a csv

I have a script on my computer that work. I've put this script on docker and put it on a server.

Then with python3 i have this error :

Traceback (most recent call last):
  File "importcsv.py", line 1067, in <module>
    for row in spamreader:
  File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 17: ordinal not in range(128)

If anyone have any idea ...

here is the extract of the code :

tab = []
with open('companyindex.csv', newline='') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=';', quotechar='"')
     i = 0
     for row in spamreader:
        i += 1
        if i > 3 :
            tab.append(row)
            #print(row)
company_index(tab)
tab = []

Upvotes: 0

Views: 243

Answers (1)

Emmanuel DUMAS
Emmanuel DUMAS

Reputation: 710

you should precise encoding in open :

tab = []
with open('companyindex.csv', newline='', encoding='utf8') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=';', quotechar='"')
     i = 0
     ...

Upvotes: 3

Related Questions