xml.parsers.expat and utf-8

D

dagurp

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?
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

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
 
K

Kent Johnson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top