Python/environment problem with char encoding/decoding

Discussion in 'Python' started by Gabriel Rossetti, Feb 18, 2009.

  1. Hello everyone,

    I originally posted this on the Twisted mailing list, but now it seams
    to be a more general python/environment problem. If I run the attached
    example in Eclipse, it works, if I run it from a terminal, it doesn't, I
    get :

    $ python xml_parser_test.py
    Traceback (most recent call last):
    File "xml_parser_test.py", line 30, in <module>
    res = rawXmlToElement("<t>reçu</t>")
    File "xml_parser_test.py", line 21, in __call__
    tmp.addRawXml(s)
    File "/usr/lib/python2.5/site-packages/twisted/words/xish/domish.py",
    line 538, in addRawXml
    self.children.append(SerializedXML(rawxmlstring))
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5:
    ordinal not in range(128)

    Does anyone understand why it doesn't work outside of Eclipse? My OS is
    Linux (Ubuntu 8.04).

    Thank you,
    Gabriel
    Gabriel Rossetti, Feb 18, 2009
    #1
    1. Advertising

  2. Gabriel Rossetti

    Peter Otten Guest

    Gabriel Rossetti wrote:

    > Hello everyone,
    >
    > I originally posted this on the Twisted mailing list, but now it seams
    > to be a more general python/environment problem. If I run the attached
    > example in Eclipse, it works, if I run it from a terminal, it doesn't, I
    > get :
    >
    > $ python xml_parser_test.py
    > Traceback (most recent call last):
    > File "xml_parser_test.py", line 30, in <module>
    > res = rawXmlToElement("<t>reçu</t>")
    > File "xml_parser_test.py", line 21, in __call__
    > tmp.addRawXml(s)
    > File "/usr/lib/python2.5/site-packages/twisted/words/xish/domish.py",
    > line 538, in addRawXml
    > self.children.append(SerializedXML(rawxmlstring))
    > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5:
    > ordinal not in range(128)
    >
    > Does anyone understand why it doesn't work outside of Eclipse? My OS is
    > Linux (Ubuntu 8.04).


    On the contrary, I don't understand why it would work in Eclipse ;)

    addRawXml(s) seems to be a fancy way to create a unicode string, and
    unicode(s) will work either if s is a bytestring that doesn't contain any
    non-ascii characters or if s is a unicode string (at least these are the
    relevant cases here).

    Try changing your program to use a unicode literal:

    # -*- coding: utf-8 -*-
    ....
    if ...
    res = rawXmlToElement(u"<t>reçu</t>")

    Peter
    Peter Otten, Feb 18, 2009
    #2
    1. Advertising

  3. Peter Otten wrote:
    > Gabriel Rossetti wrote:
    >
    >
    >> Hello everyone,
    >>
    >> I originally posted this on the Twisted mailing list, but now it seams
    >> to be a more general python/environment problem. If I run the attached
    >> example in Eclipse, it works, if I run it from a terminal, it doesn't, I
    >> get :
    >>
    >> $ python xml_parser_test.py
    >> Traceback (most recent call last):
    >> File "xml_parser_test.py", line 30, in <module>
    >> res = rawXmlToElement("<t>reçu</t>")
    >> File "xml_parser_test.py", line 21, in __call__
    >> tmp.addRawXml(s)
    >> File "/usr/lib/python2.5/site-packages/twisted/words/xish/domish.py",
    >> line 538, in addRawXml
    >> self.children.append(SerializedXML(rawxmlstring))
    >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5:
    >> ordinal not in range(128)
    >>
    >> Does anyone understand why it doesn't work outside of Eclipse? My OS is
    >> Linux (Ubuntu 8.04).
    >>

    >
    > On the contrary, I don't understand why it would work in Eclipse ;)
    >
    > addRawXml(s) seems to be a fancy way to create a unicode string, and
    > unicode(s) will work either if s is a bytestring that doesn't contain any
    > non-ascii characters or if s is a unicode string (at least these are the
    > relevant cases here).
    >
    > Try changing your program to use a unicode literal:
    >
    > # -*- coding: utf-8 -*-
    > ...
    > if ...
    > res = rawXmlToElement(u"<t>reçu</t>")
    >
    > Peter
    > --
    >
    >

    Hello Peter, that works, thanks! I was sure I had tried that at some
    point, but I must have had another problem that made me remove it. Thank
    you again!
    Gabriel
    Gabriel Rossetti, Feb 18, 2009
    #3
    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,115
    Natty Gur
    Jun 25, 2003
  2. terry
    Replies:
    2
    Views:
    2,441
    terry
    Nov 3, 2003
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,046
    Ian Collins
    May 9, 2006
  4. flyingco

    URL decoding/encoding problem

    flyingco, Nov 23, 2006, in forum: C Programming
    Replies:
    4
    Views:
    633
    flyingco
    Nov 27, 2006
  5. Replies:
    3
    Views:
    732
Loading...

Share This Page