python修改xml各个节点的值
# coding=utf-8
import os
import os.path
import xml.dom.minidom
"""修改xml路径"""
def path_1(FindPath,name1,file_name):#获取⽂件夹地址,name1:标签名
if not os.path.isdir(file_name): # 判断是否是⽂件夹,不是⽂件夹才打开
# print(file_name) # 打印獲取⽂件名
# 读取xml⽂件
dom = xml.dom.minidom.parse(os.path.join(FindPath, file_name))#解析dom节点
root = dom.documentElement
label= ElementsByTagName(name1)#标签列表
return dom,label
def xiugai_path(label):
for i in range(len(label)): # 遍歷⽂件列表⾧度
print("打印所有路径",label[i].firstChild.data)#打印所有路径
if label[i].firstChild.data[0:62] == "/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/":
#print("/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/" + path_1[i].firstChild.data[
# 62:-3] + "jpg", 1111111111)
#从新赋值
python处理xml文件label[i].firstChild.data = "/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/" + label[
i].firstChild.data[
62:-3] + "jpg"
elif label[i].firstChild.data[:28] == "E:\my_own_search\phone\step1":
#print("/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/" + path_1[i].firstChild.data[29:],
# 22222222222222)
# 从新赋值
label[i].firstChild.data = "/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/" + label[i].firstChild.data[ 29:]
elif label[i].firstChild.data.split(".")[1]!="jpg":#判断图⽚后缀是否是jpg
label[i].firstChild.data =label[i].firstChild.data.split(".")[0]+".jpg"
# print("ok,地址:{}".format(label[i].firstChild.data))
else:
# print("可能需要修改值,如:后缀等",label[i].firstChild.data)
print(None)
return len(label)
#修改neme
def xiugai_name(label,t_name=None):
a,b=0,0
c=0
for i in range(len(label)):
c+=1
print("打印所有name",label[i].firstChild.data,"⼀个XML有⼏个name:",c)
if label[i].firstChild.data=="camera":#判断name是否camera,是修改,不是往下执⾏
if t_name:
label[i].firstChild.data = "camera1"
a+=1#为镜头
elif label[i].firstChild.data=="0":
label[i].firstChild.data="phone"#从新赋值
b+=1#为⼿机
else:
print(label[i].firstChild.data)#不是上⾯两种是那种?看看
return a,b
#将修改后的xml⽂件保存
def baocun(xml_path,dom,file_name):#傳⼊保存⽂件路徑和⽂件名
with open(os.path.join(xml_path, file_name), 'w') as fh:
dom.writexml(fh)
print('写⼊{}/pose OK!'.format(file_name[0:-4]))
if __name__=="__main__":
#获得⽂件夹中所有⽂件
#获得⽂件夹中所有⽂件
FindPath = '/media/hanqing/AI开发/mubiaojiance/sj1'
#要保存地址
xml_path='/media/hanqing/AI开发/mubiaojiance/sj1'
data="name"#需要修改标签name
a,b=0,0
FileNames = os.listdir(FindPath) # 獲取⽂件内容列表
print(len(FileNames))
for file_name in FileNames:#循環讀取⽂件名
# print(file_name)#XML打印⽂件名
list_1=path_1(FindPath,data,file_name)
c=xiugai_name(list_1[1],1)##修改xml的name
# print(c)
# a+=c[0]#标签name数量
# b+=c[1]#⽂件数量
# xiugai_path(list_1[1]) # 1/解开注释修改路径⽅法
baocun(xml_path,list_1[0],file_name)#2/保存数据
# print("镜头数量为:{}".format(a),"⼿机标签数量为:{}".format(b),"xml总数:{}".format(len(FileNames)))
# s = []
# a,b,c=0,0,0
# for file_name in list_1[1]:
# print(file_name)
# if not os.path.isdir(file_name): # 判断是否是⽂件夹,不是⽂件夹才打开
# print(file_name)#打印獲取⽂件名
#读取xml⽂件
# print(os.path.join(FindPath,file_name))#打印獲取⽂件路徑
# dom = xml.dom.minidom.parse(os.path.join(FindPath,file_name))
# root = dom.documentElement
# # 获取标签对name之间的值
# name = ElementsByTagName('name')
# #獲取標籤路徑path
# path_ElementsByTagName('path')
# print(len(name))
# for i in range(len(path_1)):#遍歷⽂件列表⾧度
# #if name[i] .firstChild.data== 'screw cap':
# ##
# if path_1[i].firstChild.data[0:62]=="/home/hanqing/SSD-Tensorflow-master/VOC2019/Annotations/phone/":
# # print("/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/" + path_1[i].firstChild.data[
# # 62:-3] + "jpg", 1111111111)
# path_1[i].firstChild.data = "/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/"+path_1[i].firstChild.data[62:-3]+"jpg" # elif path_1[i].firstChild.data[:28]=="E:\my_own_search\phone\step1":
# # print("/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/" + path_1[i].firstChild.data[29:],
# # 22222222222222)
# path_1[i].firstChild.data ="/home/hanqing/SSD-Tensorflow-master/VOC2019/JPEGImages/phone/"+path_1[i].firstChild.data[29:]
# print(len(path_1[i].firstChild.data))#⾧度
# print(path_1[i].firstChild.data)
# path_1[i].firstChild.data =path_1[i].firstChild.data#name[i].firstChild.data
# print ('修改后的 path')
# print (path_1[i].firstChild.data)
# print(len("/home/hanqing/SSD-Tensorflow-master/VOC2019/Annotations/"))
# c+=1
# if name[i].firstChild.data=="phone":
# a+=1
# else:
# b+=1
# print(a,b,a+b,c)
##将修改后的xml⽂件保存
# with open(os.path.join(xml_path, file_name), 'w') as fh:
# dom.writexml(fh)
# print('写⼊name/pose OK!')
注释相对⽐较详细,可以依托照葫芦画瓢修改其他节点信息,主要实现对于xml中name、path值的修改
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论