encoding problem

Mike Driscoll kyosohma at gmail.com
Fri May 16 17:19:41 EDT 2008


On May 16, 3:31 pm, Luis Zarrabeitia <ky... at uh.cu> wrote:
> Hi, guys.
> I'm trying to read an xml file and output some of the nodes. For that, I'm
> doing a
> print node.toprettyxml()
>
> However, I get this exception:
>
> ===
>     out.write(tag.toxml())
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xba' in position
> 190: ordinal not in range(128)
> ===
>
> That happens if I "print" it, or send it to stdout, or send it to a file.
>
> How can I fix it?
> cat file works perfectly, and I'm using an utf8 terminal.
>
> I'm particularly puzzled that it won't work even if I write to a file opened
> in "b" mode. Worst thing is... I don't really need that character, just a
> general idea of how the document looks like.
>
> --
> Luis Zarrabeitia (aka Kyrie)
> Fac. de Matemática y Computación, UH.http://profesores.matcom.uh.cu/~kyrie


I recommend studying up on Python's Unicode methods and the codecs
module. This site actually talks about your specific issue though and
gives pointers:

http://evanjones.ca/python-utf8.html

HTH

Mike



More information about the Python-list mailing list