import os
import json
path = r'C:\Users\jimkongHo\Desktop\1\2'
path1 = r'C:\Users\jimkongHo\Desktop\1\pic'
json_list = os.listdir(path)
json_listn = os.listdir(path1)
for jsonfn in json_listn:
if os.path.splitext(jsonfn)[1] == '.json':
print(jsonfn)
with open(os.path.join(path1,jsonfn),'r',encoding='utf-8')as fn:
contentn = json.load(fn)['shapes'][-1]
print(contentn)
for jsonf in json_list:
if os.path.splitext(jsonf)[1] == '.json':
print(jsonf)
with open(os.path.join(path,jsonf),'r',encoding='utf-8')as f:
content = json.load(f)['shapes'][-1]
content.update(contentn)
with open(os.path.join(path,jsonf),'a',encoding='utf-8')as fl:
json.dump(content,fl,indent=4)
上面的代码能成功把一个json的label数据追加到另一个json中,但我想要追加的位置在shapes里面,请问如何实现?追加的错误结果如下:
{
"version": "4.5.10",
"flags": {},
"shapes": [
{
"label": "1",
"points": [
[
4445.142857142857,
3789.7142857142853
],
[
5002.285714285714,
4432.5714285714275
]
],
"group_id": null,
"shape_type": "circle",
"flags": {}
}
],
"imagePath": "DSC00001.JPG",
"imageData": null,
"imageHeight": 6336,
"imageWidth": 9504,
"id": "02"
}{
"label": "object",
"points": [
[
4030.857142857142,
1175.4285714285706
],
[
4687.999999999999,
1718.2857142857138
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
}
我想要的效果如下:
{
"version": "4.5.10",
"flags": {},
"shapes": [
{
"label": "1",
"points": [
[
4445.142857142857,
3789.7142857142853
],
[
5002.285714285714,
4432.5714285714275
]
],
"group_id": null,
"shape_type": "circle",
"flags": {}
},
{
"label": "object",
"points": [
[
4030.857142857142,
1175.4285714285706
],
[
4687.999999999999,
1718.2857142857138
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
}
],
"imagePath": "DSC00001.JPG",
"imageData": null,
"imageHeight": 6336,
"imageWidth": 9504,
"id": "02"
}