Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnicodeDecodeError reading string in CSV

I'm having a problem reading some chars in python.

I have a csv file in UTF-8 format, and I'm reading, but when script read:

Preußen Münster-Kaiserslautern II

I get this error:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 515, in __call__
    handler.get(*groups)
  File "/Users/fermin/project/gae/cuotastats/controllers/controllers.py", line 50, in get
    f.name = unicode( row[1])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)

I tried to use Unicode functions and convert string to Unicode, but I haven't found the solution. I tried to use sys.setdefaultencoding('utf8') but that doesn't work either.

like image 941
fermin Avatar asked Mar 22 '26 16:03

fermin


1 Answers

Try the unicode_csv_reader() generator described in the csv module docs.

like image 136
Andrea Spadaccini Avatar answered Mar 25 '26 05:03

Andrea Spadaccini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!