Jython: How to import escaped Unicode and export utf-8?

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Apr 30 10:16:46 EDT 2001


Maurice Bauhahn <bauhahnm at clara.net> writes:

> Could you, Martin, or anyone else on the list, give a short Jython script
> which imports escaped unicode from a file (presumeably using codecs) and
> exports utf-8 to a file? My attempts at the same have not been successful.

Not sure what you mean by "importing" here. The file that you want to
read - is it Python source code (which you would load using the import
statement), or is a plain data file.

If it is a plain data file, why do you insist on storing it in
unicode-escaped encoding? I recommend to store it in UTF-8

>>> x=u"A test:\u1780, done."
>>> import codecs
>>> out=codecs.open("foo.txt","w","utf-8")
>>> out.write(x)
>>> out.close()
>>> infile=codecs.open("foo.txt","r","utf-8") 
>>> infile.read()
u'A test:\u1780, done.'
>>> ^D

So it works fine for me.

OTOH, you seem to have lists that you want to store; I recommend using
pickle for that.

Regards,
Martin



More information about the Python-list mailing list