Unicode File I/O Grief
Martin v. Loewis
martin at v.loewis.de
Thu May 16 16:40:48 EDT 2002
"Bjorn Pettersen" <BPettersen at NAREX.com> writes:
> > I simply want to do the following:
> >
> > file('C:\\odd.txt', 'wb').write(ucString)
> >
> > ...so that I can examine the contents of this (rather long)
> > Unicode string in an editor.
> >
> > Can I write it to a file?
>
> I'm far from a Unicode expert, but I believe you need to say
> ucString.encode(), optionally passing which encoding you want.
Correct. Assuming that the original .write failed, .encode without
parameters won't help either, since Python already tried the system
default encoding in the .write.
I recommend to do
codecs.open('C:\\odd.txt', 'wb', encoding="utf-8").write(ucString)
or
file('C:\\odd.txt', 'wb').write(ucString.encode("utf-8"))
Regards,
Martin
More information about the Python-list
mailing list