[XML-SIG] [OT] working unicode strings

Lars Marius Garshol larsga@garshol.priv.no
28 Jun 2001 17:00:14 +0200


* Alexandre Fayolle
| 
| So, I have a UTF8 encoded unicode string, that I got it by reading a text
| node in a DOM. I want to write it somewhere on the disk. 
| 
| s= u'été'
| f=open('/tmp/foo','w')
| f.write(s)

The problem with this is that Python doesn't know how to write
non-ASCII characters into a file, since it doesn't know what encoding
to use. The solution is to use a codec that writes to the encoding you
want to use:

import codecs

s= u'été'
f=codecs.open('/tmp/foo','w',"utf-8")
f.write(s)
 
| So the first question is, how do you manage to work with unicode
| strings in PyXML/4Suite (a pointer to the right source file is a
| valuable answer).

Once you find out about the codecs module and the unicode function (in
__builtins__) it's actually quite easy. One makes occasional gaffes,
of course, but it's really not very hard.

--Lars M.