[Tutor] UnicodeDecodeError

thehouse.be at me.com thehouse.be at me.com
Mon Mar 16 10:11:59 EDT 2020


> > > 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 writing to a text file I get the same error:
Traceback (most recent call last):
File "/Users/chrisvanroey/mu_code/collab_comment_decoder.py", line 79, in <module>
p.write_text('- ' + (cel))
File "/Users/travis/build/mu-editor/mu_portable_python_macos/python/lib/python3.6/pathlib.py", line 1214, in write_text
UnicodeEncodeError: 'ascii' codec can't encode character '\xeb' in position 144: ordinal not in range(128)


So probably I have also to put  ‘utf-8’ somewhere in the p.write_text code.
But I can’t find where….
Can you help?

Here is the code:

## maak een txt bestand (ipv print)
import os
from pathlib import Path
os.chdir('/users/chrisvanroey/Desktop’)
p = Path(‘output.txt’)

# write header
p.write_text('COLLAB COMMENTS’)
p.write_text(‘/n’)

# write commentaar x personen voor elke vraag
for kolom in range(3,62,2):
    p.write_text(collabData[0][kolom-1])
    for rij in range(1,aantalResp+1):
        cel = collabData[rij][kolom]
        if cel != “”:
            p.write_text('- ' + (cel))    <<<<<<<<< HER I GET THE UNICODE ERROR
    p.write_text('————————————————————————————————‘)

# print additionele feedback x personen
p.write_text('Additional feedback:’)
for rij in range(1,aantalResp+1):
    cel = collabData[rij][62]
    if cel != “”:
        p.write_text('- ' + (cel))





On 16 Mar 2020, 02:28 +0100, thehouse.be--- via Tutor <tutor at python.org>, wrote:
> Dear Sibylle,
>
> Thank you so much.
> Adding encoding=‘utf-8’ is indeed the solution.
>
> Extra info: I was working in the mu-editor when I encountered the problem.
> I tried the same original commands (without ‘utf-8’) in Idle… and there it worked.
>
> Again thank you for solving my problem,
> Chris
> On 15 Mar 2020, 20:05 +0100, Sibylle Koczian <nulla.epistola at web.de>, wrote:
> > 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
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list