Skip to content Skip to sidebar Skip to footer

Python: How Do I Read And Parse A Unicode Utf-8 Text File?

I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example: txtFile =

Solution 1:

That file is not UTF-8; it's UTF-16LE with a byte-order marker.

Solution 2:

That is a BOM

EDIT, from the coments, it seems to be a utf-16 bom

codecs.open('foo.txt', 'r', 'utf-16')

should work.

Solution 3:

Expanding on Johnathan's comment, this code should read the file correctly:

import codecs
txtFile = codecs.open( 'halout.txt', 'r', 'utf-16' )
for line in txtFile:
   printrepr( line )

Solution 4:

Try to see if the excel file has some blank rows (and then has values again), that might cause the unexpected error.

Post a Comment for "Python: How Do I Read And Parse A Unicode Utf-8 Text File?"