批量删除/annots文件夹中所有XML文件的<occluded>0</occluded>节点
解决方案
给你两种实用的批量处理方法,按需选择:
方法一:Shell命令(Linux/macOS适用)
用sed命令直接批量操作,安全高效还自带备份:
cd /annots sed -i.bak '/<occluded>0<\/occluded>/d' *.xml
-i.bak:修改文件时会自动生成带.bak后缀的原文件备份,确认处理无误后,可执行rm *.bak删除所有备份/<occluded>0<\/occluded>/d:精准匹配包含目标节点的行并删除
方法二:Python脚本(跨平台通用)
如果习惯用Python,这个脚本可以在Windows、Linux、macOS下运行:
import os # 目标文件夹路径 folder_path = '/annots' # 遍历文件夹下所有.xml文件 for filename in os.listdir(folder_path): if filename.endswith('.xml'): file_full_path = os.path.join(folder_path, filename) # 读取文件内容 with open(file_full_path, 'r', encoding='utf-8') as f: lines = f.readlines() # 过滤掉包含目标节点的行 filtered_content = [line for line in lines if '<occluded>0</occluded>' not in line] # 写回原文件 with open(file_full_path, 'w', encoding='utf-8') as f: f.writelines(filtered_content)
执行脚本前确保Python环境正常,运行后会直接覆盖原文件;若需要备份,可在写回前添加文件复制逻辑。
处理后XML示例
<annotation> <folder></folder> <filename>Glass217_jpg.rf.92a572df568fd3d2ce118bfb5ffa6cb5.jpg</filename> <path>Glass217_jpg.rf.92a572df568fd3d2ce118bfb5ffa6cb5.jpg</path> <source> <database>roboflow.ai</database> </source> <size> <width>640</width> <height>640</height> <depth>3</depth> </size> <segmented>0</segmented> <object> <name>Glass</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <bndbox> <xmin>55</xmin> <xmax>372</xmax> <ymin>248</ymin> <ymax>406</ymax> </bndbox> </object> </annotation>
内容的提问来源于stack exchange,提问作者Emere
相关产品推荐
相关产品推荐

