minidom

Sylvain Thenault syt at pegasus.logilab.fr
Fri Sep 14 12:06:47 CEST 2001


On Fri, 14 Sep 2001, Bernhard Trummer <bernhard.trummer at divis.at> wrote:
>My XML-file looks like:
>----------------------------------------------------------------
><?xml version="1.0" encoding ="ISO-8859-1"?>
><messages version="102">
>  <message>
>   <TXT>Eisenstadt: Europaplatz, zwischen Bürgerspitalgasse und
>Kurzwiesenweg: </TXT>
>  </message>
>  <message>
>    <TXT>A2 Autobahn:</TXT>
>  </message>
>...
></messages>
>----------------------------------------------------------------
>
>The error message:
>----------------------------------------------------------------
>  File "F:\\Scripts\XMLTest2.py", line 54, in handlePoint
>    print "<li>%s</li>" % getText(point.childNodes)
>UnicodeError: ASCII encoding error: ordinal not in range(128)
>----------------------------------------------------------------
>
>The phython code (where it crashes)
>----------------------------------------------------------------
>def handlePoint(point):
>    print "<li>%s</li>" % unicode(getText(point.childNodes))
>
>def getText(nodelist):
>    rc = ""
>    for node in nodelist:
>        if node.nodeType == node.TEXT_NODE:
>            rc = rc + node.data
>    return rc
>----------------------------------------------------------------

You have this error because by default, python guess that your string
contains only ASCII characters, so when you try to encode it, it raise 
an 'ASCII encoding error'.

You have to say that your string contains latin1 characters for unicode
translation, and then encode it a printable form
the following should work:
---
def handlePoint(point):
    print "<li>%s</li>" % \
    unicode(getText(point.childNodes), 'iso8859-1').encode('iso8859-1')
---

as you see, the same thing is available for translation from unicode string
to latin1 string.
   
Hope it helps

-- 
Sylvain Thenault

LOGILAB




More information about the Python-list mailing list