【发布时间】: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 格式不正确。开始根没有右括号
>。这将在parse上引发错误。
标签: python xml attributes