Pythonxml.etree.ElementTree如果attrib值为整数,则引发ParseError

2024-06-14 20:45:11 发布

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

我试图解析一个只有几个attrib值的xml是整数。在这种情况下,python ElementTree类会引发ParseError

在xml.etree.ElementTree.ParseError:格式不正确(无效标记):第120行,列 umn 32号

这是我的xml

<Rectangle
  leftTopX = 0
  leftTopY = 0
  rightBottomX = 20
  rightBottomY = 40
/>

关于如何避免这个ParseError有什么建议吗?在

以下面的方式修改attrib值可以解决我的问题。但是我有多个xml文件要解析。更改属性值将花费更多时间。在

^{pr2}$

Tags: 标记格式情况整数xmletreeelementtreeattrib
1条回答
网友
1楼 · 发布于 2024-06-14 20:45:11

您可以切换到BeautifulSoup解析器,这在形式良好方面更为宽容。示例:

from bs4 import BeautifulSoup


data = """
<Rectangle
  leftTopX = 0
  leftTopY = 0
  rightBottomX = 20
  rightBottomY = 40 />
"""

soup = BeautifulSoup(data)
print soup.rectangle

印刷品:

^{pr2}$

您也可以将其与lxml解析器一起使用(您需要安装lxml):

^{3}$

希望有帮助。在

相关问题 更多 >