首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python从xml中提取属性值

如何使用python从xml中提取属性值
EN

Stack Overflow用户
提问于 2020-03-14 09:44:50
回答 1查看 261关注 0票数 0

如何从box子元素访问Car。我能够获得属性名,但要获得Car,需要面对问题

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-14 10:17:27

见下文

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60681467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档