[Tutor] Python - XML: How to write UNICODE to a file ?? (when using LATIN-1 Chars)

Alexandre Ratti alex at gabuzomeu.net
Tue Aug 26 11:17:30 EDT 2003

Hi Javier,

Javier JJ wrote:

>I have an xml file with ASCII characters (no encoding is specified; it
>contains characters valid in the latin-1 charset - it's a log file
>generated by MSN Messenger 6.0).
>I am processing it with mindom as follows:
>doc = minidom.parse(log_file.xml)

> Now "listado" has a xml-looking unicode string; it looks quite fine to
>me... but when I try to write it to disk, I get:
>>>>Traceback (most recent call last):
>>>>  File "<pyshell#34>", line 1, in -toplevel-
>>>>    out.write(listado)
>>>>UnicodeEncodeError: 'ascii' codec can't encode character '\ued' in
>>>>position 2274: ordinal not in range(128)
The string is being converted to the default encoding, ASCII. As ASCII 
does not support "extended" characters (eg. accented chars), you get an 
error. Try to encode the string before writing it:

# Latin-1 encoding
s = listado.encode('iso-8859-1')
# UTF-8 encoding
s = listado.encode('utf-8')

Also, take a look at the "codecs" module for related tools.



More information about the Tutor mailing list