"encoding specified in XML declaration is incorrect"

"Martin v. Löwis" martin at v.loewis.de
Thu Dec 2 16:20:14 EST 2004


Gustaf Liljegren wrote:
>     f.write(header + self.all + footer)
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 
> 745-751: ordinal not in range(128)
> 
> The XML declaration should be enough to tell the encoding. 

Sure, but that does not help at all. self.all is a Unicode string;
information about its original encoding is not available anymore.
If you want to write self.all to f, you need to encode it explicitly,
e.g.

   f.write(header + self.all.encode("koi8-r") + footer)

Instead of koi8-r, you should use the enccoding of f, of course.

> u = unicode(s, "utf-8")
> xml.sax.parseString(u, MyParser())

This is not really supposed to work (yet). You need to pass
byte strings to xml.sax.parseString, not Unicode strings.

Regards,
Martin



More information about the Python-list mailing list