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++读写方式差不多


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值