encoding problem

Discussion in 'Python' started by Luis Zarrabeitia, May 16, 2008.

  1. 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
     
    Luis Zarrabeitia, May 16, 2008
    #1
    1. Advertising

  2. On May 16, 3:31 pm, Luis Zarrabeitia <> 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
     
    Mike Driscoll, May 16, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slade

    Problem encoding/decoding image

    Slade, Jun 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,127
    Natty Gur
    Jun 25, 2003
  2. Hardy Wang

    Encoding.Default and Encoding.UTF8

    Hardy Wang, Jun 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    18,931
    Jon Skeet [C# MVP]
    Jun 9, 2004
  3. Replies:
    1
    Views:
    23,446
    Real Gagnon
    Oct 8, 2004
  4. Greg
    Replies:
    9
    Views:
    829
    Nobody
    Oct 8, 2011
  5. Replies:
    2
    Views:
    389
Loading...

Share This Page