如果 XML 文件的名称属性 <,>,",' 中包含禁止字符,我如何读取该 XML 文件? XML 超过 30k 行,目标是 pandas.dataframe
<rows>
<row number="164" item="9860404" name="160-30 Bracket" qty="1"/>
<row number="164" item="9860405" name="200-30 <> Bracket" qty="1" />
<row number="164" item="9860406" name="250-30 3/4" Bracket" qty="3" />
<row number="164" item="9860407" name="315-30 <-> Bracket" qty="4"/>
</rows>
最佳答案
您可以使用 HTMLParser
解析示例数据来自 lxml.etree
的解析器:
>>> from lxml import etree
>>> parser = etree.HTMLParser()
>>> doc =etree.parse(open('data.xml'), parser=parser)
>>> [elem.get('name') for elem in doc.xpath('//row')]
['160-30 Bracket', '200-30 <> Bracket', '250-30 3/4', '315-30 <-> Bracket']
请注意,使用 HTML 解析器解析数据会将文档包装在 <html>
中。和<body>
元素,使文档结构最终看起来像:
<html><body><rows>
<row number="164" item="9860404" name="160-30 Bracket" qty="1"/>
<row number="164" item="9860405" name="200-30 <> Bracket" qty="1"/>
<row number="164" item="9860406" name="250-30 3/4" bracket="" qty="3"/>
<row number="164" item="9860407" name="315-30 <-> Bracket" qty="4"/>
</rows>
</body></html>
关于Python-读取格式不正确的xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59586925/