How to deal with not well-formed character in xml file with elementtree in python
elementtree, python, xml, xml-parsing
Solution
Since `xml.parsers.expat.ParserCreate` supports only four encodings I would try them all. Those encodings are: `UTF-8`, `UTF-16`, `ISO-8859-1` (`Latin1`), and `ASCII` .
You can now run `ElementTree.parse` with the encoding like:
from xml.etree.ElementTree import ElementTree
from xml.parsers import expat
tree = ElementTree()
root = tree.parse(xml_file, parser=expat.ParserCreate('UTF-8') )
root = tree.parse(xml_file, parser=expat.ParserCreate('UTF-16') )
root = tree.parse(xml_file, parser=expat.ParserCreate('ISO-8859-1') )
root = tree.parse(xml_file, parser=expat.ParserCreate('ASCII') )
Problem
I'm parsing the xml files encoded with `utf-16` using `ElementTree.parse` function. The program would break down when the file contains some not well-formed characters such as `♀, ♂` .etc. And the error "`xml.parsers.expat.ExpatError: not well-formed (invalid token)`"occurs. How could I avoid this error and resolve this problem? How could I just ignore these not well-formed characters? Thanks! below is my code: ``` tree = ElementTree() root = tree.parse(xml_file) ``` xml_file is the file encoded in UTF-16 format. The error would point out the line and column number of the not well-formed character.