Py3: Read file with Unicode characters

Martin v. Loewis martin at v.loewis.de
Thu Apr 8 13:04:38 EDT 2010


Gnarlodious wrote:
> On Apr 8, 9:14 am, "Martin v. Loewis" wrote:
> 
>> When opening the file, you need to specify the file encoding.
> 
> OK, I had tried this:
> 
> open(path, 'r').read().encode('utf-8')

No, when *opening* the file, you need to specify the encoding:

open(path, 'r', encoding='utf-8').read()

> Sorry, doing Unicode in Py3 has really been a challenge.

That's because you need to re-learn some things.

Regards,
Martin



More information about the Python-list mailing list