encoding="utf8" ignored when parsing XML

Peter Otten __peter__ at web.de
Tue Dec 27 10:25:51 EST 2016

Skip Montanaro wrote:

> I am trying to parse some XML which doesn't specify an encoding (Python
> 2.7.12 via Anaconda on RH Linux), so it barfs when it encounters non-ASCII
> data. No great surprise there, but I'm having trouble getting it to use
> another encoding. First, I tried specifying the encoding when opening the
> file:
> f = io.open(fname, encoding="utf8")
> root = xml.etree.ElementTree.parse(f).getroot()
> but that had no effect. 

Isn't UTF-8 the default?

Try opening the file in binary mode then:

with io.open(fname, "rb") as f:
    root = xml.tree.ElementTree.parse(f).getroot()

More information about the Python-list mailing list