我有以下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>
我想从文件中提取某些元素。例如,在object下,有三个名称'person‘、'person’和'people‘。我使用以下方法来提取'bndbox‘值:
box = {e.tag: int(e.text) for e in root.findall('.//bndbox/*')}以下哪项输出:
{'x': 420, 'y': 226, 'w': 26, 'h': 41}但是,当我使用相同的方法查找'name‘时,我得到了以下输出:
label = {e.tag: e.text for e in root.findall('.//name')}
{'name': 'people'}这似乎只是输出最终的值。
任何建议都将不胜感激。
发布于 2019-06-21 16:50:08
尝尝这个
[name.text for name in root.findall('object/name')]发布于 2019-06-21 18:35:58
这里(基于etree的工作代码)
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)输出
['person', 'person', 'people']
[['457', '217', '31', '78'], ['486', '217', '29', '78'], ['420', '226', '26', '41']]https://stackoverflow.com/questions/56699542
复制相似问题