Python目标检测-合并两个xml文件 “object”属性

import os
import xml.etree.ElementTree as ET
from shutil import copy

class Tools:
    def __init__(self, xmls1, xmls2):
        self.xmls1 = xmls1
        self.xmls2 = xmls2
        
    def read_from_xml(self):
        xml1_dict = {}
        xml2_dict = {}
        for root, dir_list, file_list in os.walk(self.xmls1):
            for index, file_fn in enumerate(file_list):
                xml_fn = os.path.join(root, file_fn)
                xml1_dict[file_fn] = xml_fn

        for root, dir_list, file_list in os.walk(self.xmls2):
            for index, file_fn in enumerate(file_list):
                xml_fn = os.path.join(root, file_fn)
                xml2_dict[file_fn] = xml_fn
                
        for key, value in xml2_dict.items():
            if key in xml1_dict:
                tree1 = ET.parse(value)
                tree2 = ET.parse(xml1_dict[key])
                
                root1 = tree1.getroot()
                root2 = tree2.getroot()
                
                for object in root2.findall('object'):
                    root1.append(object)
                tree1.write('xml/{}'.format(key))
            else:
                copy(value, 'xml')
                
        for key, value in xml1_dict.items():
            if key not in xml2_dict:
                copy(value, 'xml')
                

if __name__ == '__main__':
    tool = Tools('xml1', 'xml2')
    tool.read_from_xml()
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值