0

I have a xml file (a1.xml).

  <object>
    <label id="0">Helmet</label>
    <bndbox id="0">
      <xmin>295</xmin>
      <ymin>304</ymin>
    </bndbox>

    <label id="1">Person</label>
    <bndbox id="1">
      <xmin>279</xmin>
      <ymin>291</ymin>
    </bndbox>
  </object>

I want to get this format

obj = [{'label': 'Helmet', 'xmin': 295, 'ymin': 304},
       {'label': 'Person', 'xmin': 279, 'ymin': 291}]

my code as following

import xml.etree.cElementTree as ET
tree = ET.parse('a1.xml')
for subtag in root.findall('object'):
    tag = subtag.findall('label').text
    x = subtag.findall('xmin').text
    y = subtag.findall('ymin').text

How to solve this?

1 Answer 1

1

Maybe using XPath syntax helps here. Clean coding would also check if the find()-results are not None, e.g.

import xml.etree.cElementTree as ET
tree = ET.parse('a1.xml')
labels = tree.findall('.//label')
for label in labels:
    label_id = label.get('id')
    # find the first bndbox with id set to the same value
    bndbox = tree.find('.//bndbox[@id="%s"]' % label_id)
    if bndbox is None:
        continue
    tag = label.text
    if bndbox.find('xmin') and bndbox.find('ymin'):
        x = bndbox.find('xmin').text
        y = bndbox.find('ymin').text
    else:
        continue
    # here you can store tag, x, y in a list or dictionary
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.