【问题标题】:Get all same attribute values for XML in python在python中获取XML的所有相同属性值
【发布时间】:2021-04-29 21:35:00
【问题描述】:

我试图从“TextRegion--> Coords”标签中获取所有“点”属性值。我不断从中得到错误。注意:有名为“TextRegion”和“ImageRegion”的标签都包含“Coords”。但是,我只想要“TextRegion”中的坐标点。

请帮忙!谢谢!!

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<PcGts xmlns="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15"
    <Metadata>
        <Creator/>
        <Created>2021-01-24T17:11:35</Created>
        <LastChange>1969-12-31T19:00:00</LastChange>
        <Comments/>
    </Metadata>
    <Page imageFilename="0004.png" imageHeight="3655" imageWidth="2493">
        <TextRegion id="r1" type="paragraph">
            <Coords points="1653,146 1651,148"/>
            <TextEquiv>
                <Unicode/>
            </TextEquiv>
        </TextRegion>
        <TextRegion id="r2" type="paragraph">
            <Coords points="2071,326 2069,328 2058,328 2055"/>
            <TextEquiv>
                <Unicode/>
            </TextEquiv>
        </TextRegion>
        <ImageRegion id="r3">
            <Coords points="443,621 443,2802 2302,2802 2302,621"/>
        </ImageRegion>
        <TextRegion id="r4" type="paragraph">
            <Coords points="2247,2825 2247,2857 2266,2857 2268,2860 2268"/>
            <TextEquiv>
                <Unicode/>
            </TextEquiv>
        </TextRegion>
        <TextRegion id="r5" type="paragraph">
            <Coords points="731,2828 731,2839 728,2841"/>
            <TextEquiv>
                <Unicode/>
            </TextEquiv>
        </TextRegion>
    </Page>
</PcGts>

这是我的代码:

from lxml import etree as ET

tree = ET.parse('0004.xml')
root = tree.getroot()
print(root.tag)

for tag in root.find_all('Page/TextRegion/Coords'):
    value = tag.get('points')
    print(value)

【问题讨论】:

  • 您的 XML 格式不正确。开始根没有右括号&gt;。这将在parse 上引发错误。

标签: python xml attributes


【解决方案1】:

假设您发布的 XML 是缺少关闭根元素打开的复制/粘贴问题,您的另一个主要问题是经典的 XML 解析问题,该问题涉及在默认命名空间下解析节点,其中包括以 xmlns 开头的任何属性 没有冒号分隔的前缀,例如xmlns:doc="..."

因此,您需要在 Python 中定义一个临时命名空间前缀来解析每个命名元素,您可以使用传入findall(而不是find_all)的字典来执行此操作。

from lxml import etree as ET

tree = ET.parse('0004.xml')
nsmp = {'doc': 'http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15'}

root = tree.getroot()
print(root.tag)

# SPECIFY NAMESPACE AND PREFIX ALL NAMED ELEMENTS
for tag in root.findall('doc:Page/doc:TextRegion/doc:Coords', namespaces=nsmp):
    value = tag.get('points')
    print(value)

# 1653,146 1651,148
# 2071,326 2069,328 2058,328 2055
# 2247,2825 2247,2857 2266,2857 2268,2860 2268
# 731,2828 731,2839 728,2841

顺便说一下,lxml 是一个功能丰富的 XML 库(需要 3rd 方安装),它在其他强大的工具中支持完整的 XPath 1.0。只需将import 行更改为from xml.etree import ElementTree as ET,上述代码仍然可以与Python 的内置etree 一起使用。

但是lxml extends this library比如直接用xpath解析成属性:

tree = ET.parse('0004.xml')

# SPECIFY NAMESPACE AND PREFIX ALL NAMED ELEMENTS
for pts in tree.xpath('//doc:Coords/@points', namespaces=nsmp):
    print(pts)

# 1653,146 1651,148
# 2071,326 2069,328 2058,328 2055
# 2247,2825 2247,2857 2266,2857 2268,2860 2268
# 731,2828 731,2839 728,2841

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2010-09-10
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多