首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在XML文件中查找元素

在XML文件中查找元素
EN

Stack Overflow用户
提问于 2019-06-21 16:22:32
回答 2查看 294关注 0票数 0

我有以下XML文件:

代码语言:javascript
复制
<annotation>
  <folder>KAIST Multispectral Ped Benchmark</folder>
  <filename>set00/V003/I00397</filename>
  <source>
    <database>KAIST pedestrian</database>
    <annotation>KAIST pedestrian</annotation>
    <image>KAIST pedestrian</image>
    <url>https://soonminhwang.github.io/rgbt-ped-detection/</url>
    <note>Sanitized training annotation [BMVC18] (https://li-chengyang.github.io/home/MSDS-RCNN/)</note>
  </source>
  <size>
    <width>640</width>
    <height>512</height>
    <depth>4</depth>
  </size>
  <segmented>0</segmented>
  <object>
    <name>person</name>
    <bndbox>
      <x>457</x>
      <y>217</y>
      <w>31</w>
      <h>78</h>
    </bndbox>
    <pose>unknown</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <occlusion>0</occlusion>
  </object>
  <object>
    <name>person</name>
    <bndbox>
      <x>486</x>
      <y>217</y>
      <w>29</w>
      <h>78</h>
    </bndbox>
    <pose>unknown</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <occlusion>0</occlusion>
  </object>
  <object>
    <name>people</name>
    <bndbox>
      <x>420</x>
      <y>226</y>
      <w>26</w>
      <h>41</h>
    </bndbox>
    <pose>unknown</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <occlusion>0</occlusion>
  </object>
</annotation>

我想从文件中提取某些元素。例如,在object下,有三个名称'person‘、'person’和'people‘。我使用以下方法来提取'bndbox‘值:

代码语言:javascript
复制
box = {e.tag: int(e.text) for e in root.findall('.//bndbox/*')}

以下哪项输出:

代码语言:javascript
复制
{'x': 420, 'y': 226, 'w': 26, 'h': 41}

但是,当我使用相同的方法查找'name‘时,我得到了以下输出:

代码语言:javascript
复制
label = {e.tag: e.text for e in root.findall('.//name')}
{'name': 'people'}

这似乎只是输出最终的值。

任何建议都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2019-06-21 16:50:08

尝尝这个

代码语言:javascript
复制
[name.text for name in root.findall('object/name')]
票数 0
EN

Stack Overflow用户

发布于 2019-06-21 18:35:58

这里(基于etree的工作代码)

代码语言:javascript
复制
import xml.etree.ElementTree as ET

xml = '''<annotation>
  <folder>KAIST Multispectral Ped Benchmark</folder>
  <filename>set00/V003/I00397</filename>
  <source>
    <database>KAIST pedestrian</database>
    <annotation>KAIST pedestrian</annotation>
    <image>KAIST pedestrian</image>
    <url>https://soonminhwang.github.io/rgbt-ped-detection/</url>
    <note>Sanitized training annotation [BMVC18] (https://li-chengyang.github.io/home/MSDS-RCNN/)</note>
  </source>
  <size>
    <width>640</width>
    <height>512</height>
    <depth>4</depth>
  </size>
  <segmented>0</segmented>
  <object>
    <name>person</name>
    <bndbox>
      <x>457</x>
      <y>217</y>
      <w>31</w>
      <h>78</h>
    </bndbox>
    <pose>unknown</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <occlusion>0</occlusion>
  </object>
  <object>
    <name>person</name>
    <bndbox>
      <x>486</x>
      <y>217</y>
      <w>29</w>
      <h>78</h>
    </bndbox>
    <pose>unknown</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <occlusion>0</occlusion>
  </object>
  <object>
    <name>people</name>
    <bndbox>
      <x>420</x>
      <y>226</y>
      <w>26</w>
      <h>41</h>
    </bndbox>
    <pose>unknown</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <occlusion>0</occlusion>
  </object>
</annotation>'''

root = ET.fromstring(xml)
names = [n.text for n in root.findall('.//object/name')]
print(names)
boxes = [[box.find('x').text, box.find('y').text, box.find('w').text, 
          box.find('h').text] for box in
          root.findall('.//object/bndbox')]
print(boxes)

输出

代码语言:javascript
复制
['person', 'person', 'people']
[['457', '217', '31', '78'], ['486', '217', '29', '78'], ['420', '226', '26', '41']]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56699542

复制
相关文章

相似问题

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