最近需要处理xml文件,学习并整理了一些常用的操作,代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/7/9 15:16 # @Author : xiaodai import xml.etree.ElementTree as ET from lxml import etree from tqdm import tqdm import os def creat_xml(xml_file,x,y,w,h): """ 新建xml文件 :param xml_file: xml :param x: xmin :param y: ymin :param w: 宽 :param h: 高 :return: """ root = etree.Element("annotation") # 创建根节点 annotation child1 = etree.SubElement(root, "folder") child1.text = "VOC2007" object = etree.SubElement(root, "object") # 创建annotation节点的子节点 object namen = etree.SubElement(object, "name") # 创建object节点的子节点 name namen.text = 'person' # name的值为person bndbox = etree.SubElement(object, "bndbox") xminn = etree.SubElement(bndbox, "xmin") xminn.text = str(x) yminn = etree.SubElement(bndbox, "ymin") yminn.text = str(y) xmaxn = etree.SubElement(bndbox, "xmax") xmaxn.text = str(x + w) ymaxn = etree.SubElement(bndbox, "ymax") ymaxn.text = str(y + h) weight = etree.SubElement(bndbox, "box_w") weight.text = str(w) height = etree.SubElement(bndbox, "box_h") height.text = str(h) tree = etree.ElementTree(root) tree.write(xml_file, pretty_print=True, xml_declaration=False, encoding='utf-8') def edit_xml(xml_file): """ 修改xml文件 :param xml_file:xml文件的路径 :return: """ tree = ET.parse(xml_file) objs = tree.findall('object') for ix, obj in enumerate(objs): name = obj.find('name').text if name == 'mouse': print(xml_file) obj_new = obj.find('bndbox') xmin = str( 1280 - int(obj_new.find('xmin').text)) xmax = str( 1280 - int(obj_new.find('xmax').text)) obj_new.find('xmin').text = xmin # 修改节点值 obj_new.find('xmax').text = xmax conf = obj_new.find('conf') conf.tag = 'fenshu' # 修改节点名 newEle = ET.Element("NewElement1") newEle.text = "new element" obj_new.append(newEle) # 新增节点 newEle = obj_new.find('xmin') obj_new.remove(newEle) # 删除节点 obj_new.set('key','value') # 给bndbox节点设置属性 <bndbox key="value"> tree.write(xml_file,method='xml',encoding='utf-8') # 更新xml文件 if __name__ == '__main__': path = r'F: emp mp_0709' xml_files = [os.path.join(rootdir, file) for rootdir, _, files in os.walk(path) for file in files if (file.endswith('.xml'))] for xml in tqdm(xml_files): edit_xml(xml) # 修改xml文件 for i in tqdm(range(10)): xml = os.path.join(path,str(i)+'.xml') creat_xml(xml,32,120,80,80) # 新建xml文件