0

How i can access Car from box subelement. I am able to get the attribute name but facing issue to get the Car

<annotations>
      <image height="940" id="0" name="90.jpg" width="1820">
        <box label="Objects" occluded="1" xbr="255" xtl="0" ybr="624" ytl="509">
         <attribute name="Class">Car</attribute>
         <attribute name="Occlusion %">25-50%</attribute>
         <attribute name="Truncation %">0%</attribute>
        </box>
      </image>
    </annotations>
2

1 Answer 1

2

See below

import xml.etree.ElementTree as ET


xml = '''<annotations>
      <image height="940" id="0" name="90.jpg" width="1820">
        <box label="Objects" occluded="1" xbr="255" xtl="0" ybr="624" ytl="509">
         <attribute name="Class">Car</attribute>
         <attribute name="Occlusion %">25-50%</attribute>
         <attribute name="Truncation %">0%</attribute>
        </box>
      </image>
    </annotations>'''

#
# Find the attribute element (under box element) where the attribute name value is 'Class'. print the text of the element text
#
root = ET.fromstring(xml)
print(root.find(".//box/attribute[@name='Class']").text)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @balderman solution works form me, i was trying to access with single ./

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.