python修改json文件

需要解析的json文件示例

{
 "info": {
    "contributor": "",
    "description": "",
    "url": "",
    "year": "",
    "version": "",
    "date_created": ""
  },
"annotations": [
    {
      "bbox": [ 222.61, 495.35, 101.76999999999998, 94.37 ],
      "image_id": 0,
      "iscrowd": 0,
      "segmentation": [ [ 222.61, 495.35, 324.38, 495.35, 324.38, 589.72, 222.61, 589.72 ] ],
      "area": 9604.034899999999,
      "category_id": 1,
      "id": 1
    },
    {
      "bbox": [ 566.77, 637.83, 464.44000000000005, 357.12 ],
      "image_id": 0,
      "iscrowd": 0,
      "segmentation": [ [ 566.77, 637.83, 1031.21, 637.83, 1031.21, 994.95, 566.77, 994.95 ] ],
      "area": 165860.8128,
      "category_id": 3,
      "id": 2
    },
    {
      "bbox": [ 226.09, 867.86, 163.45000000000002, 203.54000000000008 ],
      "image_id": 0,
      "iscrowd": 0,
      "segmentation": [ [ 226.09, 867.86, 389.54, 867.86, 389.54, 1071.4, 226.09, 1071.4 ] ],
      "area": 33268.61300000002,
      "category_id": 2,
      "id": 3
    },
    {
      "bbox": [ 328.08, 536.06, 27.75, 61.06000000000006 ],
      "image_id": 1,
      "iscrowd": 0,
      "segmentation": [ [ 328.08, 536.06, 355.83, 536.06, 355.83, 597.12, 328.08, 597.12 ] ],
      "area": 1694.4150000000016,
      "category_id": 2,
      "id": 4
    },
    {
      "bbox": [ 194.85, 550.86, 49.96000000000001, 46.25999999999999 ],
      "image_id": 1,
      "iscrowd": 0,
      "segmentation": [ [ 194.85, 550.86, 244.81, 550.86, 244.81, 597.12, 194.85, 597.12 ] ],
      "area": 2311.1495999999997,
      "category_id": 2,
      "id": 5
    },
    {
      "bbox": [ 309.58, 661.88, 114.72000000000003, 170.23000000000002 ],
      "image_id": 1,
      "iscrowd": 0,
      "segmentation": [ [ 309.58, 661.88, 424.3, 661.88, 424.3, 832.11, 309.58, 832.11 ] ],
      "area": 19528.785600000007,
      "category_id": 2,
      "id": 6
    } ],
    "images": [
    {
      "width": 1920,
      "coco_url": "",
      "height": 1080,
      "license": 0,
      "flickr_url": "",
      "file_name": "frame_000000",
      "date_captured": 0,
      "id": 0
    },
    {
      "width": 1920,
      "coco_url": "",
      "height": 1080,
      "license": 0,
      "flickr_url": "",
      "file_name": "frame_000001",
      "date_captured": 0,
      "id": 1
    },
    {
      "width": 1920,
      "coco_url": "",
      "height": 1080,
      "license": 0,
      "flickr_url": "",
      "file_name": "frame_000002",
      "date_captured": 0,
      "id": 2
    },
    {
      "width": 1920,
      "coco_url": "",
      "height": 1080,
      "license": 0,
      "flickr_url": "",
      "file_name": "frame_000003",
      "date_captured": 0,
      "id": 3
    },
    {
      "width": 1920,
      "coco_url": "",
      "height": 1080,
      "license": 0,
      "flickr_url": "",
      "file_name": "frame_000004",
      "date_captured": 0,
      "id": 4
    }]
...
}

1.删除

当某一标签值不满足条件时,删除
以下删除category_id=2的标注信息

load_dict['annotations'] = [i for i in load_dict['annotations'] if i['category_id'] != 2]

2.修改

     for i in load_dict['annotations']:
         if i['category_id'] == 3:
             i['category_id'] = 2

完整代码

import json
import os

path = 'D:/achenf/data/0618/data0706/fold2/asd'
dirs = os.listdir(path)

num_flag = 0
for file in dirs: # 循环读取路径下的文件并筛选输出
    if os.path.splitext(file)[1] == ".json": # 筛选csv文件
        num_flag = num_flag + 1

        print("path ===== ",file)
        print(os.path.join(path,file))
        with open(os.path.join(path,file),'r') as load_f:
            load_dict = json.load(load_f)
        print("load_dict['annotations']:")
        print(len(load_dict['annotations']))

        # 删除
       #  load_dict['annotations'] = [i for i in load_dict['annotations'] if i['category_id'] != 2]
       
        # step2 将taxi3标签改为2
        count = 0
        for i in load_dict['annotations']:
           if i['category_id'] == 3:
               i['category_id'] = 2
               count = count+1
        print(count)

      
        with open(os.path.join(path,file),'w') as dump_f:
            json.dump(load_dict, dump_f)

        # print(len(load_dict['annotations']))
if(num_flag == 0):
    print('所选文件夹不存在json文件,请重新确认要选择的文件夹')
else:
    print('共{}个json文件'.format(num_flag))


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值