python批量拼接两个xml文件中的目标内容


脚本书写背景

深度学习项目中,肯定会遇到这种情况,比如有两个单独的数据集(数据集图片相同或者有重复的数据),第一个数据集标签是睁眼、闭眼;另外一个数据集标签为打哈欠、人脸,标签格式都为voc格式,即xml文件。这时,我们不需要再任何一个数据集上再次标注想要的目标,而是可以通过python脚本,把两类xml合并成1个,操作非常的方便灵活,大大提高我们标注效率。一下是python脚本源码,欢迎使用~


提示:以下是本篇文章正文内容,下面案例可供参考

一、完整程序如下

from xml.etree.ElementTree import ElementTree, Element, parse
import xml.etree.ElementTree as ET
import os
import shutil

hole_path = r'F:\Red_Green_Light_data\A_s\rgyt_data\want_xml'
arm_path = r'F:\Red_Green_Light_data\A_s\rgyt_data\img363_xml'
out_path = r'F:\Red_Green_Light_data\A_s\rgyt_data\img363_xml2'

# 格式化
def __indent(elem, level=0):
    i = "\n" + level*"\t"
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "\t"
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            __indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i


for hole_xml in os.listdir(hole_path):
    # 将同名xml合并
    if os.path.exists(os.path.join(arm_path,hole_xml)):
        print('fusing',hole_xml)
        tree_hole = parse(os.path.join(hole_path,hole_xml))
        root_hole = tree_hole.getroot()  # annotation

        new_hole = tree_hole

        tree_arm = parse(os.path.join(arm_path,hole_xml))
        root_arm = tree_arm.getroot()  # annotation
        object = (tree_arm.findall('object'))
        for i in range(len(object)):
            root_hole.append(object[i])
        __indent(root_hole)
        new_hole.write(os.path.join(out_path,hole_xml))
    # 不同名xml复制
    else:
        print('copying',hole_xml)
        shutil.copy(os.path.join(hole_path,hole_xml), out_path)


# 将不同名xml复制
for arm_xml in os.listdir(arm_path):
    if not os.path.exists(os.path.join(out_path,arm_xml)):
        print('copying')
        shutil.copy(os.path.join(arm_path, arm_xml), out_path)

总结

以上就是今天要讲的内容,脚本超级好用,学好python,大大提高工作效率!觉得还不错的,感谢关注收藏,后续还会继续分享好用的数据处理脚本。

在Linux中,可以使用Python脚本来复制文件。你可以使用osshutil这两个Python模块来实现。首先,你需要导入这两个模块:import shutil import os。然后你可以使用shutil模块的copy函数来复制文件。这个函数接受两个参数,第一个参数是要复制的文件的路径,第二个参数是目标文件夹的路径。你可以使用os模块的path.join函数来拼接文件路径。下面是一个复制文件的示例代码: import shutil import os def copy_file(source_file, target_folder): shutil.copy(source_file, target_folder) source_file = "/path/to/source/file.txt" target_folder = "/path/to/target/folder" copy_file(source_file, target_folder) 这个示例代码中,source_file是要复制的文件的路径,target_folder是目标文件夹的路径。你只需要将这两个路径替换成你自己的路径即可。然后,调用copy_file函数,传入source_file和target_folder作为参数,就可以将文件复制到指定文件夹了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [使用Python复制Linux文件到指定文件夹](https://blog.youkuaiyun.com/gaoyueace/article/details/88648421)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Linux下创建python脚本并运行(入门)](https://blog.youkuaiyun.com/weixin_30822467/article/details/116654807)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [xml文件批量处理python脚本](https://download.youkuaiyun.com/download/caoxinri123/88239057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

onnx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值