Python读写xml,合并两个xml⽂件object节点,并完成缩进问题
import os
ElementTree as ET
ElementTree import Element
import gflags
import sys
from PIL import Image
Flags = gflags.FLAGS
gflags.DEFINE_string(
'source_dir1', 'C:/Users/Administrator/Desktop/test/test1', 'source_dir1')
gflags.DEFINE_string(
'source_dir2', 'C:/Users/Administrator/Desktop/test/test2', 'source_dir2')
gflags.DEFINE_string(
'result_dir', 'C:/Users/Administrator/Desktop/test/result', 'result_dir')
def copy_img(inputpath,outputpath):
for rt ,folders,files in os.walk(inputpath):
for folder in folders:
folder_path = os.path.join(rt,folder)
files = os.listdir(folder_path)
for file in files:
dswith('jpg'):
dir= outputpath + '/' + folder
oldpath = os.path.join(folder_path,file)
newpath = os.path.join(dir, file)
img = Image.open(oldpath)
img.save(newpath)
print('save successfully %s'%newpath)
def delect_node(inputpath,sku):
for rt ,folder,files in os.walk(inputpath):
for file in files:
dswith('xml'):
filepath = os.path.join(rt,file)
tree = ET.parse(filepath)
root = t()
for objs in root.findall('object'):
for obj in objs:
for name in obj.iter('name'):
name =
if  (name==sku):
print('delect seccessful %s objedt'%name)
#ve(obj)
tree.write(filepath, encoding='utf-8', xml_declaration=True)
def get_filename_or_path(filename,inputpath):
for rt ,folder,files in os.walk(inputpath):
for file in files:
dswith('xml'):
filepath = os.path.join(rt,file)
tree = ET.parse(filepath)
root = t()
fn = root.find('filename').text
if  (fn==filename):
return(fn,filepath)
def prettyXml(element, indent, newline, level=0):  # elemnt为传进来的Elment类,参数indent⽤于缩进,newline⽤于换⾏
if element:  # 判断element是否有⼦元素
if element:  # 判断element是否有⼦元素
== None isspace():  # 如果element的text没有内容
< = newline + indent * (level + 1)
else:
< = newline + indent * (level + 1) + strip() + newline + indent * (level + 1)
# else:  # 此处两⾏如果把注释去掉,Element的text也会另起⼀⾏
# = newline + indent * (level + 1) + strip() + newline + indent * level
temp = list(element)  # 将elemnt转成list
for subelement in temp:
if temp.index(subelement) < (len(temp) - 1):  # 如果不是list的最后⼀个元素,说明下⼀个⾏是同级别元素的起始,缩进应⼀致            subelement.tail = newline + indent * (level + 1)
else:  # 如果是list的最后⼀个元素,说明下⼀⾏是母元素的结束,缩进应该少⼀个
subelement.tail = newline + indent * level
prettyXml(subelement, indent, newline, level=level + 1)  # 对⼦元素进⾏递归操作
def write_xml(box,name,skuname,root,tree,outpath):
element = Element('object')
one1 = Element('bndbox')
two1 = Element('xmin')
two2 = Element('ymin')
two3 = Element('xmax')
two4 = Element('ymax')
< = box[0]
< = box[1]
< = box[2]
< = box[3]
name_node = Element('name')
skuName_node = Element('skuName')
= name
= skuname
one1.append(two1)
one1.append(two2)
one1.append(two3)
one1.append(two4)
element.append(one1)
getsavefilenameelement.append(name_node)
element.append(skuName_node)
root.append(element)
prettyXml(root, '\t', '\n')
print('save successful %s'%outpath)
tree.write(outpath, encoding='utf-8', xml_declaration=True)
def read_xml(inputpath1,inputpath2,outputhpath):
for rt ,folders,files in os.walk(inputpath1):
for folder in folders:
folder_path = os.path.join(rt,folder)
files = os.listdir(folder_path)
for file in files:
dswith('xml'):
filepath = os.path.join(folder_path,file)
dir= outputhpath + '/' + folder
if not ists(dir):
os.makedirs(dir)
outpath = dir + '/' +file
tree = ET.parse(filepath)
root = t()
filename1 = root.find('filename').text
filename,filepath2 = get_filename_or_path(filename1,inputpath2)
tree2 = ET.parse(filepath2)
root2 = t()
box = [0,0,0,0]
name = 0
skuname = 0
for objs in root.iter('object'):
for objs in root.iter('object'):
for obj in objs:
for bnd in obj.iter('bndbox'):
j = 0
for i in bnd:
box[j] = i.text
j+=1
for name in obj.iter('name'):
name =
for skuname in obj.iter('skuName'):
skuname =
write_xml(box, name, skuname, root2, tree2, outpath)
def main(argv):
Flags(argv)
sku = '4000000000001'
#delect_sult_dir, sku)
read_xml(Flags.source_dir1,Flags.source_sult_dir)    copy_img(Flags.source_dir1, sult_dir)
if __name__ == '__main__':
main(sys.argv)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。