read a unicode file

Alan Kennedy alanmk at hotmail.com
Mon Jun 9 12:18:37 EDT 2003


Pete Shinners wrote:

> i have a text file of unicode data. i'm not sure how to read this from a
> file. i can read() it into a massive python string, but cannot get it to
> convert to unicode. it's definitely inefficient to read it all at once,
> but i expect the files i'll be reading aren't too large to cause problems.

Pete,

I think that this is one way to solve the problem.

#-----------------

import codecs

f = codecs.open(filename, "rt", "utf-16")
data = f.read()
f.close

# 'data' should now contain unicode data.

#-----------------

Also, the standard iterator interface seems to be supported by the codecs.open
method as well, e.g.

for line in codecs.open(filename, mode, encoding):
    print line

HTH,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list