首先上代码:

def handle_delete_tag_in_xml(patch_file_path, replace_tag):
    """
    针对xml中整段的代码删除,
    xml文件中不适用上述的整理以注释的方式单行准确删除;
    故考虑整段标签式删除
    :param patch_file_path:文件路径
    :param replace_tag:使用的替换特定标签
    :return:
    """
    print ("start>>>>>handle_delete_tag_in_xml {} for {}".format(replace_tag, patch_file_path))
    with open(patch_file_path, 'r') as patch_file:
        content = patch_file.readlines()

    with open(patch_file_path, 'w+') as f:
        meet_tag_count = 0
        for (index, line) in enumerate(content):
            if replace_tag in line:
                print ("find {} in line {}".format(replace_tag, index))
                meet_tag_count = meet_tag_count+1
            if meet_tag_count%2 == 0:
                # 取余,奇数则保留,偶数则删除
                if not replace_tag in line:
                    f.write(line)
    print ("end>>>>>handle_delete_tag_in_xml {} for {}".format(replace_tag, patch_file_path))

如下图所示的区域:
在这里插入图片描述
对其进行上述方法处理之后,该框内的代码将会被移除!

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐