xml.dom.minidom character encoding

C. Benson Manica cbmanica at gmail.com
Wed Apr 21 14:40:01 EDT 2010

On Apr 21, 2:25 pm, Peter Otten <__pete... at web.de> wrote:

> Are you sure that your script has
> str = u"..."
> like in your post and not just
> str = "..."

No :-)

str=u"<?xml version=\"1.0\" encoding=\"utf-8\"?><elements><elem attrib=
doc=xml.dom.minidom.parseString( str.encode("utf-8") )
xml=doc.toxml( encoding="utf-8")
file=codecs.open( "foo.xml", "w", "utf-8" )
file.write( xml )


  File "./demo.py", line 12, in <module>
    file.write( xml )
  File "/usr/lib/python2.5/codecs.py", line 638, in write
    return self.writer.write(data)
  File "/usr/lib/python2.5/codecs.py", line 303, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
62: ordinal not in range(128)

but dropping the encoding argument to doc.toxml() seems to finally
work.  I'd be curious to know why the code you posted (that worked for
you) didn't for me, but at this point I'm just happy with something
functional.  Thank you very kindly!

More information about the Python-list mailing list