Forum Archive

Problem reading ascii text

uncertaintea

I have a script that reads an ascii encoded text file, created within the Pythonista environment. However, when i execute the script on the text file, i get “error ascii codec cant decode byte 0xe2 in position 2154: ordinal n...”
Tracking the bug seems to show it to be an error not in .read()

I try the same script and text file and 20 others like it that i have created on my computer using the stock IDE, and I get no error.

How can I deal with this?

I can provide code and text files to recreate the error.

ClackHack

try using encode='utf-8' in the open() function

Mor3ris

This is a third-party file, and I get a new one every day, so I would rather not change it !

ccc

Try running your script under Python 2.7 instead of Python 3.

Legacy Python did not make a distinction between bytes and str while Python 3 strictly enforces that distinction.

uncertaintea

@ClackHack This works, thanks. I still don’t really understand what was going on to cause the encoding error. Isn’t ascii the same as utf-8?

mikael

@uncertaintea, the difference between the two is something I recommend learning a little bit about. See e.g. this article.