xml文件示例:
<annotation verified="no">
<folder>crossroad</folder>
<filename>000329</filename>
<path>D:\测试\人脸\crossroad\000329.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>1920</width>
<height>1080</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>face</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<Difficult>0</Difficult>
<bndbox>
<xmin>1735</xmin>
<ymin>98</ymin>
<xmax>1884</xmax>
<ymax>280</ymax>
</bndbox>
</object>
<object>
<name>face</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<Difficult>0</Difficult>
<bndbox>
<xmin>789</xmin>
<ymin>480</ymin>
<xmax>838</xmax>
<ymax>537</ymax>
</bndbox>
</object>
</annotation>
解析xml,并写入txt
#coding=utf-8 import xml.dom.minidom as xmldom import os def openXml(filepath,txtpath): for root,dirs,files in os.walk(filepath): for file in files: if os.path.splitext(file)[1] == '.xml': str1 = root.split("\\")[1] path = os.path.join(root,file) # 打开xml文档 dom = xmldom.parse(path) #得到根节点 annotation = dom.documentElement filename = annotation.getElementsByTagName("filename")[0].childNodes[0].data str1 = str1 + "/" + filename objects = annotation.getElementsByTagName("object") for object in objects: xmin = object.getElementsByTagName("xmin")[0].childNodes[0].nodeValue ymin = object.getElementsByTagName("ymin")[0].childNodes[0].nodeValue xmax = object.getElementsByTagName("xmax")[0].childNodes[0].nodeValue ymax = object.getElementsByTagName("ymax")[0].childNodes[0].nodeValue str1 = str1 + " " + xmin + " " + ymin + " " + xmax + " " + ymax str1 = str1 + "\n" #以追加的形式写入txt f = open(txtpath,"a+") f.write(str1) f.close() if __name__ == '__main__': #xml所在文件夹路径 filepath = "H:\crossroad" #txt路径 txtpath = "H:\crossroad\data.txt" openXml(filepath,txtpath)