当我使用xmltodict加载下面的xml文件时,我得到一个错误: xml.parsers.expat.ExpatError:不是格式良好的(无效标记):第1行,第1列
这是我的文件:
<?xml version="1.0" encoding="utf-8"?>
<mydocument has="an attribute">
<and>
<many>elements</many>
<many>more elements</many>
</and>
<plus a="complex">
element as well
</plus>
</mydocument>来源:
import xmltodict
with open('fileTEST.xml') as fd:
xmltodict.parse(fd.read())我使用的是Windows 10,使用Python 3.6和xmltodict 0.11.0
如果我使用ElementTree,它可以工作
tree = ET.ElementTree(file='fileTEST.xml')
for elem in tree.iter():
print(elem.tag, elem.attrib)
mydocument {'has': 'an attribute'}
and {}
many {}
many {}
plus {'a': 'complex'}注意:我可能遇到了一个新的行问题。
Note2:我在两个不同的文件上使用了超越比较。
它在UTF-8 BOM编码的文件上崩溃,并在UTF-8文件上工作。
UTF-8 BOM是一个字节序列(EF、BB、BF),它允许读者识别以UTF-8编码的文件。
https://stackoverflow.com/questions/48821725
复制相似问题