适用场景:
例如:适用于在数据标注时有部分标签名错误或需要更改、删除某一指定类别
修改指定类别名称
import os.path
import xml.dom.minidom
# xml文件存放路径
path = 'C:/Users/soli/Desktop/1'
# 定义需替换的类名['原始类别名称','新名称']
name = ['a', 'b']
files = os.listdir(path) # 返回文件夹中的文件名列表
for xmlFile in files:
dom = xml.dom.minidom.parse(path + '\\' + xmlFile)
root = dom.documentElement
newfilename = root.getElementsByTagName('name')
for i, t in enumerate(newfilename):
if t.firstChild.data == name[0]:
newfilename[i].firstChild.data = name[1]
with open(os.path.join(path, xmlFile), 'w') as fh:
dom.writexml(fh)
删除指定类别
# 批量移除xml标注中的某一个类别标签
import xml.etree.cElementTree as ET
import os
# xml文件路径
xml_path = r'C:\Users\soli\Desktop\1'
xml_files = os.listdir(xml_path)
# 需要删除的类别名称
CLASSES = ["ssss"]
for axml in xml_files:
path_xml = os.path.join(xml_path, axml)
tree = ET.parse(path_xml)
root = tree.getroot()
for child in root.findall('object'):
name = child.find('name').text
if name in CLASSES:
root.remove(child)
tree.write(os.path.join(xml_path, axml))