首先上代码:
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))
如下图所示的区域:
对其进行上述方法处理之后,该框内的代码将会被移除!