Unicode File

Alex Martelli aleaxit at yahoo.com
Tue Jul 31 06:08:32 EDT 2001


"Tksee Lhsoh" <tksee at yahoo.com> wrote in message
news:3B66179D.1D55B27C at yahoo.com...
>
> I would like to open and save a file in unicode.
>
>
> For example,
> s=open("unicode_filename").read();
> seems to return oridinary string, not unicode.
>
> What should I do ?

There are several approaches, but I think the
simplest one is to use the unicode() built-in
function, giving it the codec name you want.

For example, suppose string s (e.g. as obtained
from the above operation) is:

>>> s
'\xff\xfec\x00i\x00a\x00o\x00'

e.g. as would be given by encoding UTF-16.  In
this case,

>>> unicode(s,'utf-16')
u'ciao'


As I said there are several other approaches,
but this one may be simplest for simple needs.


Alex






More information about the Python-list mailing list