xml.parsers.expat and utf-8

Discussion in 'Python' started by dagurp@gmail.com, May 3, 2005.

  1. Guest

    I have this code:

    import xml.parsers.expat
    parser = xml.parsers.expat.ParserCreate(encoding="UTF-8")
    text = unicode("<div>þórður</div>",'UTF-8')
    print parser.Parse(text,1)

    And this is what I get:
    UnicodeEncodeError: 'ascii' codec can't encode characters in position
    5-6: ordinal not in range(128)


    I think I've tried all variations possible but I always get errors.
    Anyone know what I'm doing wrong?
     
    , May 3, 2005
    #1
    1. Advertising

  2. wrote:
    > I think I've tried all variations possible but I always get errors.
    > Anyone know what I'm doing wrong?


    You are passing Unicode strings to Parse. Try byte strings instead.

    Regards,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, May 3, 2005
    #2
    1. Advertising

  3. Kent Johnson Guest

    wrote:
    > I have this code:
    >
    > import xml.parsers.expat
    > parser = xml.parsers.expat.ParserCreate(encoding="UTF-8")
    > text = unicode("<div>þórður</div>",'UTF-8')


    I think you want
    text = u"<div>þórður</div>".encode('UTF-8')
    so text is a UTF-8 string.

    Kent
     
    Kent Johnson, May 4, 2005
    #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. Will Stuyvesant

    Help with xml.parsers.expat please?

    Will Stuyvesant, Jul 4, 2003, in forum: Python
    Replies:
    1
    Views:
    697
    Alan Kennedy
    Jul 4, 2003
  2. Thomas Guettler

    xml.parsers.expat vs. xml.sax

    Thomas Guettler, Apr 27, 2004, in forum: Python
    Replies:
    2
    Views:
    907
    Martijn Faassen
    Apr 27, 2004
  3. kaens
    Replies:
    6
    Views:
    341
    Stefan Behnel
    May 23, 2007
  4. kaens
    Replies:
    0
    Views:
    384
    kaens
    May 23, 2007
  5. sharan
    Replies:
    1
    Views:
    727
    Pavel Lepin
    Oct 26, 2007
Loading...

Share This Page