簡體   English   中英

Python 在與 ElementTree 相同的循環中獲取 XML 的父值和子值

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

我手動構建了一個帶有所需注釋/圖像數據集像素坐標的 XML 文件,因此我想將這些注釋解析為單個注釋 object。 這是我的 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

這個片段給出了 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

父節點“文件”(圖像的名稱)不存在,因為我正在迭代框節點。 但是,當我用 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

我可以通過使用 2 個不同的循環並從一個循環中獲取名稱並從第二個循環中獲取其他屬性來處理此問題,但我確信應該有一種方法可以做到這一點,感謝您的幫助:) 保持安全!

編輯:關於 XML 的條款可能是錯誤的,這是我第一次使用 XML。

見下文(查找所有“圖像”元素並使用數據類進行注釋)

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

[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