error when parsing xml
Odd-R.
oddr at home.no.no
Mon Sep 5 08:51:42 EDT 2005
On 2005-09-05, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Odd-R. wrote:
>
>> I use xml.dom.minidom to parse some xml, but when input
>< contains some specific caracters(æ, ø and å), I get an
>> UnicodeEncodeError, like this:
>>
>> UnicodeEncodeError: 'ascii' codec can't encode character
>> u'\xe6' in position 604: ordinal not in range(128).
>>
>> How can I avoid this error?
>
> if you're getting this on the way in, something is broken (posting a short
> self-contained test program will help us figure out what's wrong).
This is retrieved through a webservice and stored in a variable test
<?xml version='1.0' encoding='utf-8'?>
<!-- DTD for xmltest-->
<!DOCTYPE testtest [ <!ELEMENT testtest ( test*)>
<!ELEMENT test (#PCDATA)>]>
<testtest><test>æøå</test></testtest>
printing this out yields no problems, so the trouble seems to be when executing
the following:
doc = minidom.parseString(test)
Then I get this error:
File "C:\Plone\Python\lib\site-packages\_xmlplus\dom\minidom.py", line 1918, in parseString
return expatbuilder.parseString(string)
File "C:\Plone\Python\lib\site-packages\_xmlplus\dom\expatbuilder.py", line 940, in parseString
return builder.parseString(string)
File "C:\Plone\Python\lib\site-packages\_xmlplus\dom\expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 157-159: ordinal not in range(128)
In the top of the file, I have put this statement: # -*- coding: utf-8 -*-
> if you're getting this on the way out, the problem is that you're trying to
> print Unicode strings to an ASCII device. use the "encode" method to
> convert the string to the encoding you want to use, or use codecs.open
> to open an encoded stream and print via that one instead.
Can you give an example of how this is done?
Thanks again for all help!
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve
-Jokke & Valentinerne
More information about the Python-list
mailing list