python读写xml文件

最近有做ssd打标xml框统计,顺便记录一下python的glob模块和读写xml的xml.dom.minidom模块

xml文件特性:

1、有标签对组成,<aa></aa>

2、标签可以有属性:<aa id=’123’></aa>

3、标签对可以嵌入数据:<aa>abc</aa>

4、标签可以嵌入子标签(具有层级关系):

<aa>

     <bb></bb>

</aa>

因此读写就有格式可循。

#!/usr/bin/python
#coding=utf-8
import glob
import xml.dom.minidom


still_one_files = glob.iglob( r 'F:\still_one\still_oneclean\*.xml')
still_two_files = glob.iglob( r 'F:\still_two\still_twoclean\*.xml')

still_one_num = 0
still_two_num = 0

for file in still_one_files:
with open( file) as f:
dom = xml.dom.minidom.parse(f)
root = dom.documentElement
cc = root.getElementsByTagName( 'bndbox')
num = len(cc)
still_one_num = still_one_num + num

for file in still_two_files:
with open( file) as f:
dom = xml.dom.minidom.parse(f)
root = dom.documentElement
cc = root.getElementsByTagName( 'bndbox')
num = len(cc)
still_two_num = still_two_num + num

print "still_one_num = ",still_one_num
print "still_two_num = ",still_two_num

上代码可以看出,glob模块很好用,读xml子节点直接和c++读写方式差不多


猜你喜欢

转载自blog.csdn.net/jdk_yxs/article/details/79228412