注意:
会对源文件进行修改,做好备份。
目标:
对xml中分类好的框的分类进行批量修改。
import re, os
import glob
def updateFile(file, old_str, new_str):
with open(file, "r", encoding="utf-8") as f1, open("%s.bak" % file, "w", encoding="utf-8") as f2:
for line in f1:
f2.write(re.sub(old_str, new_str, line))
os.remove(file)
os.rename("%s.bak" % file, file)
file_list = glob.glob(pathname="F:\\Dataset\\annotations\\*.xml", recursive=False) #修改到你的xml存储文件夹
for name in file_list:
updateFile(name, "old", "new") # 将old标签批量修改成new标签
该代码示例使用Python的re和os模块,针对F:Datasetannotations目录下的所有XML文件,将旧分类标签替换为新标签,执行前会创建源文件的备份。此过程涉及文件读写,正则表达式匹配以及文件重命名操作。
1万+

被折叠的 条评论
为什么被折叠?



