how to write unicode to a txt file?

Facundo Batista facundo at
Wed Jan 17 17:57:33 CET 2007

Frank Potter wrote:

> But it seems that python can't directly write unicode to a file,

You need to use the method open from module codecs:

>>> import codecs
>>> a ="pru_uni.txt", "w", "utf-8")
>>> txt = unicode("campeón\n", "utf-8")
>>> a.write(txt)
>>> a.close()

So, then, from command line:

facundo at expiron:~$ file pru_uni.txt 
pru_uni.txt: UTF-8 Unicode text



.   Facundo

More information about the Python-list mailing list