[Tutor] UnicodeDecodeError

Sibylle Koczian nulla.epistola at web.de
Sun Mar 15 14:55:22 EDT 2020


Am 15.03.2020 um 16:41 schrieb thehouse.be--- via Tutor:
> I am a beginner, learning Python.
> So sorry if my question is basic.
> 
> I am trying to work with .csv files in order to analyse data which comes from a Google Forms survey.
> Idea is to handle the raw data, do some statistical analysis and make a report.
> 
> When trying to convert the data into a listy of lists, I get a UnicodeDecodeError.
> 
> This is what I do:
> 
>>>> import csv
>>>> exampleFile = open(‘example.csv’)
>>>> exampleReader = csv.reader(exampleFile)
>>>> exampleData = list(exampleReader)
> 
> This last statement generates:
> —————————————————————————————————————
> UnicodeDecodeError Traceback (most recent call last)
> <ipython-input-9-3817c0931c6f> in <module>
> ----> 1 exampleData = list(exampleReader)
> /Applications/mu-editor.app/Contents/Resources/python/lib/python3.6/encodings/ascii.pyc in decode(self, input, final)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1798: ordinal not in range(128)
> 
> I suppose there is a bizarre character somewhere in the file, but no idea where.
> As we use accents and umlauts in our language, could that be the problem?
> If that would be the problem, how to solve?
> 

There doesn't need to be anything bizarre in the file - accents and 
umlauts suffice for ascii to choke. Opening the file with the right 
encoding given explicitly should help, for example for utf-8:

exampleFile = open('example.csv', encoding='utf-8')

HTH
Sibylle


More information about the Tutor mailing list