read a unicode file

Alan Kennedy alanmk at
Mon Jun 9 18:18:37 CEST 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.


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


import codecs

f =, "rt", "utf-16")
data =

# 'data' should now contain unicode data.


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

for line in, mode, encoding):
    print line


alan kennedy
check http headers here:
email alan:    

More information about the Python-list mailing list