参考:https://blog.csdn.net/LOVE1055259415/article/details/79166754
xml 解析与生成:https://www.cnblogs.com/jonyq/p/5929073.html
import xml.dom.minidom
dom=xml.dom.minidom.parse('/media/d_2/everyday/0727/xml/mingpai_0725_2.xml')
root=dom.documentElement
name=root.getElementsByTagName('name')
for i in range(len(name)):
if 'CJHao' == name[i].firstChild.data:
print "find___88"
name[i].firstChild.data = 'chejiahao'
with open("/media/d_2/everyday/0727/xml/change.xml",'w') as fh:
dom.writexml(fh)
print('写入name/pose OK!')
晕死,还必须另外保存一个文件才能看到更改后的,不是就地修改的
批量改xml中的filename的代码如下:
import xml.dom.minidom as xmldom
import os
root = "/media/d_2/2018biaozhushuju/20180904KeyMsg/add/20180912_hbd_jielun_qianming/xml/"
save_path = "/media/d_2/2018biaozhushuju/20180904KeyMsg/add/20180912_hbd_jielun_qianming/xml_nice/"
file_list = os.listdir(root)
for file in file_list:
new_name = file.replace(".xml",".jpg")
#print "srcname=",file
#print "new_name=",new_name
xmlfile = root + file
domobj = xmldom.parse(xmlfile)
elementobj = domobj.documentElement
subElementObj = elementobj.getElementsByTagName("filename")
subElementObj[0].firstChild.data =new_name
save_file = save_path + file
with open(save_file,"w") as fh:
domobj.writexml(fh)
print "write xml ok!"
VOC2007
mingpai_0725_2.jpg
>
hiromori2
Hiroyuki Mori
>
1227
401
3
0
废了我九牛二虎之力啊,想自己写个脚本根据xml在图上画标记的框的,看到既有的脚本,obj.getElementsByTagName("bndbox")[0].childNodes[0].nodeValue都是用这个函数一层层的解析label的,可是解析坐标xmin,xmax就是报错:xmin = box.getElementsByTagName("xmin")[0].childNodes[0].nodeValue AttributeError: 'unicode' object has no attribute 'getElementsByTagName' 看了,能解析的obj
在知乎上面找到一句话:推荐一个简单粗暴的组合:xml2dict+flatten 把xml转成orderdict,然后拍平成单层dict,管你三层还是四层,一次遍历无需递归~
然后我去下载xml2dict。真好用:
from xml2dict import XML2Dict
xml = XML2Dict()
r = xml.parse('/media/d_2/everyday/0915/HBD_JieLQianM_0914_2.xml')
#print r
for item in r.annotation:
print item
print '------------'
for item in r.annotation.object:
print item.name, item.bndbox.xmin, item.bndbox.xmax, item.bndbox.ymin, item.bndbox.ymax
##############################################################
Reloaded modules: xml2dict, object_dict
segmented
object
value
filename
source
owner
folder
size
------------
hb_qianming 1332 1491 252 384
hb_qianming 909 1040 277 370
hege 835 903 164 201
hege 1014 1079 86 122
hege 402 476 88 126
两个工具包下载地址:https://download.csdn.net/download/yang332233/10668460