深度学习常用的工具(2)
批量修改xml文件中的标签属性
"""
批量修改xml文件中的标签属性
对folder,filename和path内容进行修改,并同时去除文件名中的中文汉字
"""
import re
import os
import xml.etree.ElementTree as ET
def change_xml(xml_path):
filelist = os.listdir(xml_path)
# 打开xml文档
for xmlfile in filelist:
# print(xmlfile)
doc = ET.parse(xml_path + xmlfile)
objects = doc.findall('object')
root = doc.getroot()
folder = root.find('folder')
folder.text = 'pic'
filename = root.find('filename')
filename1 = filename.text
rstr = "[\u4e00-\u9fa5]+"
filename2 = re.sub(rstr, "", filename1)
filename.text = xmlfile.split('.')[0] + ".jpg"
path = root.find('path')
old_path = path.text
print(old_path)
new_path = 'D:\\all_element\\pic\\' + xmlfile.split('.')[0] + ".jpg"
print(new_path)
rstr = "[\u4e00-\u9fa5]+"
new_path = re.sub(rstr, "", new_path)
path.text = new_path
doc.write(xml_path + xmlfile) # 保存修改
print('修改成功!')
if __name__ == '__main__':
change_xml(r'D:/all_element/xml/')
weixin_44996884
原创文章 8获赞 1访问量 80
关注
私信
展开阅读全文