Python使用ElementTree在同一循环中获取XML的父值和子值



我手动构建了一个带有所需注释/图像数据集像素坐标的XML文件,因此我想将这些注释解析为单个注释对象。这是我的XML文件:

<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>
<images>
<image file='pngCA43_01.jpg'>
<box top='673' left='92' width='875' height='508'/>
</image>
<image file='pngCA43_02.jpg'>
<box top='680' left='79' width='885' height='501'/>
</image>
<image file='pngCA43_03.jpg'>
<box top='677' left='86' width='876' height='501'/>
</image>
<image file='pngCA43_04.jpg'>
<box top='675' left='84' width='878' height='505'/>
</image>
<image file='pngCA43_05.jpg'>
<box top='658' left='87' width='879' height='511'/>
</image>

这样持续了1000行。我想在同一个循环中访问文件、顶部、左侧、宽度和高度参数。这是我的python代码:

import xml.etree.ElementTree as ET

class Annotation():
name = ""
top = 0
left = 0
width = 0
height = 0
def __init__(self, name, top, left, width, height):
self.name = name
self.top = top
self.left = left
self.width = width
self.height = height
annotations = []
root_xml = ET.parse("xml/idcard.xml").getroot()
i = 1
for type_tag in root_xml.iter("box"):
name = type_tag.get('file')
top = type_tag.get('top')
left = type_tag.get('left')
width = type_tag.get('width')
height = type_tag.get('height')
print(f'{i}. Name: {name} Top: {top} Left: {left} Width: {width} Height: {height}n')
annotationObject = Annotation(name, top, left, width, height)
annotations.append(annotationObject)
i += 1

这个片段给出了输出:

1. Name: None Top: 673 Left: 92 Width: 875 Height: 508
2. Name: None Top: 680 Left: 79 Width: 885 Height: 501
3. Name: None Top: 677 Left: 86 Width: 876 Height: 501
4. Name: None Top: 675 Left: 84 Width: 878 Height: 505
5. Name: None Top: 658 Left: 87 Width: 879 Height: 511

父节点"file"(图像的名称(不存在,因为我正在对box节点进行迭代。但是,当我用root_xml.iter()替换root_xml.iter("box")时,输出为:

1. Name: None Top: None Left: None Width: None Height: None
2. Name: pngCA43_01.jpg Top: None Left: None Width: None Height: None
3. Name: None Top: 673 Left: 92 Width: 875 Height: 508
4. Name: pngCA43_02.jpg Top: None Left: None Width: None Height: None
5. Name: None Top: 680 Left: 79 Width: 885 Height: 501
6. Name: pngCA43_03.jpg Top: None Left: None Width: None Height: None

我可以通过使用两个不同的循环来处理这个问题,从一个循环中获取名称,从第二个循环中获得其他属性,但我相信应该有办法做到这一点,谢谢你的帮助:(保持安全!

编辑:关于XML的术语可能是错误的,这是我第一次使用XML。

见下文(查找所有"image"元素并使用数据类进行注释(

import xml.etree.ElementTree as ET
from dataclasses import dataclass
XML = '''<images>
<image file='pngCA43_01.jpg'>
<box top='673' left='92' width='875' height='508'/>
</image>
<image file='pngCA43_02.jpg'>
<box top='680' left='79' width='885' height='501'/>
</image>
<image file='pngCA43_03.jpg'>
<box top='677' left='86' width='876' height='501'/>
</image>
<image file='pngCA43_04.jpg'>
<box top='675' left='84' width='878' height='505'/>
</image>
<image file='pngCA43_05.jpg'>
<box top='658' left='87' width='879' height='511'/>
</image>
</images>'''

@dataclass
class Annotation:
name: str
top: int
left: int
width: int
height: int

root = ET.fromstring(XML)
annotations = [Annotation(i.attrib['file'], int(i.find('box').attrib['top']), int(i.find('box').attrib['left']),
int(i.find('box').attrib['width'])
, int(i.find('box').attrib['height'])) for i in root.findall('.//image')]
print(annotations)

输出

[Annotation(name='pngCA43_01.jpg', top=673, left=92, width=875, height=508), Annotation(name='pngCA43_02.jpg', top=680, left=79, width=885, height=501), Annotation(name='pngCA43_03.jpg', top=677, left=86, width=876, height=501), Annotation(name='pngCA43_04.jpg', top=675, left=84, width=878, height=505), Annotation(name='pngCA43_05.jpg', top=658, left=87, width=879, height=511)]

最新更新