简体   繁体   English

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

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

i manually constructed a XML file with desired annotations / pixel coordinates of image dataset, therefore i want to parse those annotations to single annotation object.我手动构建了一个带有所需注释/图像数据集像素坐标的 XML 文件,因此我想将这些注释解析为单个注释 object。 This is my XML file:这是我的 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>

And goes on like this for 1000 lines.像这样持续 1000 行。 I want to access file, top, left, width and height parameters in the same loop.我想在同一个循环中访问文件、顶部、左侧、宽度和高度参数。 This is my python code:这是我的 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

This snippet gives the output:这个片段给出了 output:

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

The parent node 'file' ( name of the image) is not present since i am iterating over the box node.父节点“文件”(图像的名称)不存在,因为我正在迭代框节点。 However when i replace root_xml.iter("box") with root_xml.iter() , output is:但是,当我用 root_xml.iter() 替换root_xml.iter("box") root_xml.iter() , output 是:

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

I can work with this by using 2 different loops and getting name from one loop and other attributes from second loop, but i'm sure there should be a way to do this, thanks for the help:) Stay safe!我可以通过使用 2 个不同的循环并从一个循环中获取名称并从第二个循环中获取其他属性来处理此问题,但我确信应该有一种方法可以做到这一点,感谢您的帮助:) 保持安全!

Edit: Terms about XML might've been wrong, this is my first time working with XML's.编辑:关于 XML 的条款可能是错误的,这是我第一次使用 XML。

See below (find all 'image' elements and use a dataclass for Annotation)见下文(查找所有“图像”元素并使用数据类进行注释)

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)

output output

[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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM