Python 在与 ElementTree 相同的循环中获取 XML 的父值和子值

Python getting Parent and Child Values of XML in same loop with ElementTree

我手动构建了一个 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'(图像名称)不存在,因为我正在迭代框节点。但是,当我将 root_xml.iter("box") 替换为 root_xml.iter() 时,输出为:

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

我可以通过使用 2 个不同的循环并从一个循环中获取名称并从第二个循环中获取其他属性来解决这个问题,但我确信应该有办法做到这一点,感谢您的帮助:) 保持安全!

编辑:关于 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)]