最近有做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>
因此读写就有格式可循。
扫描二维码关注公众号,回复:
1090043 查看本文章
#!/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