import os
import json
json_folder_old = './labels_old'
json_folder_new = './labels'
num = 1
for filename in os.listdir(json_folder_old):
json_path_old = os.path.join(json_folder_old, filename)
json_path_new = os.path.join(json_folder_new, filename)
print(json_path_old , ' ', num)
with open(json_path_old, 'rb') as f_load:
f_dir = json.load(f_load)
# print(f_dir, '\n')
for obj in f_dir['objects']:
# print(obj)
obj_points = obj['obj_points'] # 列表
obj_points = obj_points[0]
# print(obj_points)
obj_points["w"] = obj_points.pop("width")
obj_points["h"] = obj_points.pop("height")
# print(obj_points)
with open(json_path_new, 'w', encoding='utf-8') as f_dump:
json.dump(f_dir, f_dump,ensure_ascii=False)
num += 1
python json文档读写(含有汉子中文)
最新推荐文章于 2025-03-11 20:18:24 发布
本文介绍了一个Python脚本,用于批量修改文件夹中JSON文件的数据结构。具体操作为将'obj_points'字典中的'width'和'height'键名分别替换为'w'和'h',实现对图像标注数据的格式调整。
2717

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



