python xml不会解析

2024-04-29 02:51:41 发布

您现在位置:Python中文网/ 问答频道 /正文

试图从美国专利和贸易局读取大量数据。我尝试了几次xml files from here,得到了相同的结果:

import xml.etree.ElementTree as ET
import re
file = 'ipgb20210105.xml'
tree = ET.parse(file)

产生:“ParseError:文档元素后的垃圾:第862行,第0列”

已尝试recommendation to wrap with fake root node,但这也不起作用:

with open(file) as f:
    xml = f.read()
tree = ET.fromstring(re.sub(r"(<\?xml[^>]+\?>)", r"\1<root>", xml) + "</root>")

产生:“ParseError:格式不正确(无效令牌):第2行第2列”

非常感谢任何帮助


Tags: 数据fromimportretreeaswithroot