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

A.M. Kuchling amk at amk.ca
Thu Sep 4 14:46:15 EDT 2003


On Tue, Aug 26, 2003 at 12:18:38AM +0200, 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).
>>>> out.write(listado)
>
>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 default encoding in Python is ASCII, so characters over 127 aren't
handled by default.  If you want some other encoding (for Spanish,
Latin-1 is probably what you want), you need to convert it explicitly:

s = u'whatever ...'
output.write(s.encode('iso-8859-1'))

--amk




More information about the Tutor mailing list