【发布时间】:2018-02-16 07:10:24
【问题描述】:
当我使用 xmltodict 加载下面的 xml 文件时,出现错误: xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 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'}
注意:我可能遇到了换行问题。
注意 2:我在两个不同的文件上使用了 Beyond Compare。
它在 UTF-8 BOM 编码的文件上崩溃,并在 UTF-8 文件上工作。
UTF-8 BOM 是一个字节序列 (EF BB BF),允许阅读器将文件识别为以 UTF-8 编码。
【问题讨论】:
-
您得到的确切回溯是什么?我刚刚尝试按照您展示的方式进行操作,它对我来说可以正常工作,使用字节或 unicode(Python 3 字符串)作为输入。
-
错误是:xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 1
标签: python-3.x windows-10