程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python修改json文件

編輯:Python

需要解析的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))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved