重命名xml

将标注错的xml改名,批量修改xml文件里的标签名称

 
  
'''
通过解析xml文件,批量修改xml文件里的标签名称,比如把标签bottle改成feilinB
'''
import os.path
import glob
import xml.etree.ElementTree as ET
path = r'C:/Users/Lenovo/Desktop/B/'
for xml_file in glob.glob(path + '/*.xml'):
  tree = ET.parse(xml_file)
  root = tree.getroot()
  for member in root.findall('object'):
    objectname = member.find('name').text
    if objectname == 'bottle':    # 修改此处“bottle”
        print(objectname)
        member.find('name').text = str('feilinB')   #修改此处 “feilinB”
        tree.write(xml_file)

你可能感兴趣的:(xml,python)