ET.parse 中文
python报错
Traceback (most recent call last):
File “C:/test.py”, line 47, in
tree = ET.parse(‘C:\test.xml’)
File “C:\Python\Python38-32\lib\xml\etree\ElementTree.py”, line 1202, in parse
tree.parse(source, parser)
File “C:\Python\Python38-32\lib\xml\etree\ElementTree.py”, line 595, in parse
self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 49, column 15
对比其它正常的运行xml发现不同的是这个xml里包含了中文字符,虽然不一定非要用中文但想万一以后一定要用时怎么办就捣鼓了一下
解决办法:
把下面之前的代码
import xml.etree.ElementTree as ET
tree = ET.parse('C:\test.xml')
root = tree.getroot()
改成:
import xml.etree.ElementTree as ET
f = open('C:\test.xml')
xml_text = f.read()
root = ET.fromstring(xml_text)
f.close()