读取xml文件
假设xml文件是这样的:
<annotation>
<folder>detection</folder>
<filename>00001.png</filename>
<size>
<width>478</width>
<height>270</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>person</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>420</xmin>
<ymin>125</ymin>
<xmax>462</xmax>
<ymax>256</ymax>
</bndbox>
</object>
<object>
<name>person</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>463</xmin>
<ymin>125</ymin>
<xmax>479</xmax>
<ymax>255</ymax>
</bndbox>
</object>
<object>
<name>person</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>286</xmin>
<ymin>97</ymin>
<xmax>301</xmax>
<ymax>138</ymax>
</bndbox>
</object>
</annotation>
读取方法如下:
import xml.etree.ElementTree as ET
with open('a.xml', 'r') as f:
tree=ET.parse(f)
root = tree.getroot()
root
# <Element 'annotation' at 0x107577a98>
list(root)
# <Element 'folder' at 0x107577908>
# <Element 'filename' at 0x107577ef8>
# <Element 'size' at 0x107577868>
# <Element 'segmented' at 0x107577d18>
# <Element 'object' at 0x1075777c8>
# <Element 'object' at 0x1075775e8>
# <Element 'object' at 0x10741dcc8>
size = root.find('size') # 获取子元素
w = int(size.find('width').text) # 读取值
h = int(size.find('height').text)
for obj in root.iter('object'): # 多个元素
difficult = obj.find('difficult').text
cls = obj.find('name').text
本文最后更新于2021年5月13日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!