批量修改xml的属性值


# -*- coding: utf-8 -*-
"""
Created on Mon Sep  3 06:42:30 2018

@author: pc
"""

import os
import os.path
import xml.dom.minidom
import xml.etree.cElementTree as et
 
path="/home/pc/abc/research/object_detection/PZ/VOCdevkit/VOC2007/Annotations"
files=os.listdir(path)
s=[]

square_list = []
# =============================================================================
# extensional filename
# =============================================================================
def file_extension(path): 
    return os.path.splitext(path)[1] 

for xmlFile in files: 
    if not os.path.isdir(xmlFile): 
        if file_extension(xmlFile) == '.xml':
            print(os.path.join(path,xmlFile))
            tree=et.parse(os.path.join(path,xmlFile))
            root=tree.getroot()
#            filename=root.find('filename').text
#            print("filename is", path + '/' + xmlFile)
            for Object in root.findall('object'):
                Object.find('name').text = "ROS1"

    tree.write(os.path.join(path,xmlFile), encoding="utf-8", xml_declaration=True)

猜你喜欢

转载自blog.csdn.net/weixin_39153202/article/details/82386436