# -*- coding: utf-8 -*-
"""
Created on Sat Feb 20 14:38:01 2021
@author: zqq
"""
import glob
from xml.etree import ElementTree as ET
#批量读取Annotations下的xml文件
#per=ET.parse(r'C:\Users\rockhuang\Desktop\Annotations\000003.xml')
xml_dir=r'C:\Users\zqq\Desktop\Annotations'
xml_list= glob.glob(xml_dir + '/*.xml')
for xml in xml_list:
# print(xml)
per=ET.parse(xml)
# p_filename = per.find('filename')
# name = p_filename.text
# print(name)
# new_name = name.lower()
# print(new_name)
# name = new_name
p=per.findall('/object')
for oneper in p: #找出person节点
child=oneper.getchildren()[0] #找出person节点的子节点
if child.text=='VerticalCrack': # 将VerticalCrack改为verticalcrack
child.text='verticalcrack'
per.write(xml)
print(child.tag,':',child.text)
以上代码可以实现批量修改xml中的类别标签。
2022.4.20更新
上述代码是在python3.9版本以下才可可以使用!
附加:将 xmin,ymin,xmax,ymax坐标和width,height统一缩小,这里主要学习解析xml文件,遍历获取xml中的节点信息
# coding=utf-8
# 读xml文件中的一个rect
import xml.etree.ElementTree as ET
import os, sys
import glob
xml_dir=r'C:\Users\rockhuang\Desktop\vc_xml'
xml_list= glob.glob(xml_dir + '\*.xml')
for xml in xml_list:
tree = ET.parse(xml)
root = tree.getroot()
for size in root.findall('size'):
for width in size.iter('width'):
width.text=str(round(int(width.text)/4))
# print(width.text)
for height in size.iter('height'):
height.text=str(round(int(height.text)/4))
# print(height.text)
for ob in root.findall('object'):
for bndbox in ob.iter('bndbox'):
# for l in bndbox:
# print(l.text)
for xmin in bndbox.iter('xmin'):
xmin.text= str(round(int(xmin.text)/4))
for ymin in bndbox.iter('ymin'):
ymin.text =str(round(int(ymin.text)/4))
for xmax in bndbox.iter('xmax'):
xmax.text =str(round(int(xmax.text)/4))
for ymax in bndbox.iter('ymax'):
ymax.text = str(round(int(ymax.text)/4))
#print(xmin.text,ymin.text,xmax.text,ymax.text)
tree.write(xml)
参考
https://blog.csdn.net/weixin_38632246/article/details/90710139
https://blog.csdn.net/weixin_44604887/article/details/113183863