python - 在python中将json文件与comon键合并
问题描述
我有一些 json 文件
框架1.json:
{"annotation": {"folder": "lab_img", "filename": "frame1.jpg", "size": {"width": "1280", "height": "720", "depth": "3"}, "segmented": "0", "object": {"name": "person1", "pose": "Unspecified", "truncated": "0", "difficult": "0", "bndbox": {"xmin": "553", "ymin": "147", "xmax": "894", "ymax": "552"}}}}
时间戳
{"frame1": "0:0:0:66", "frame2": "0:0:0:100", "frame3": "0:0:0:133", "frame4": "0:0:0:166"}
预期的o/p:output.json
{"annotation": {"folder": "lab_img", "filename": "frame1.jpg", "size": {"width": "1280", "height": "720", "depth": "3"}, "segmented": "0", "object": {"name": "person1", "pose": "Unspecified", "truncated": "0", "difficult": "0", "bndbox": {"xmin": "553", "ymin": "147", "xmax": "894", "ymax": "552","frame1": "0:0:0:66"}}}}
我需要在 python 中迭代超过 10k 个 json 文件,因为我是 python 的新手,我尝试过这种方法:
import json
with open('C:\\Users\\frame1.json') as infile:
json_object = json.load(infile)
with open('C:\\Users\\time_stamp.json') as infile:
dict_object = json.load(infile)
for key in dict_object:
framenumber = int(key.strip('frame'))
for ndjs in json_object:
if ndjs['frame'] == framenumber:
# add the key/value pair
ndjs[key] = dict_object[key]
# we can break as we've found it
break
解决方案
dic1={"annotation": {"folder": "lab_img", "filename": "frame1.jpg", "size": {"width": "1280", "height": "720", "depth": "3"}, "segmented": "0", "object": {"name": "person1", "pose": "Unspecified", "truncated": "0", "difficult": "0", "bndbox": {"xmin": "553", "ymin": "147", "xmax": "894", "ymax": "552"}}}}
dic2={"frame1": "0:0:0:66", "frame2": "0:0:0:100", "frame3": "0:0:0:133", "frame4": "0:0:0:166"}
for i,j in dic2.items():
dic1["annotation"]["object"]["bndbox"].__setitem__(i,j)
break
print(dic1)
推荐阅读
- sql-server - ForeignKey SetNull OnDelete
- r - 如何在单个图中绘制不同数据集的平均线性回归
- java - Junit 无法使用 Spark Structured Streaming 创建的文件删除 @TempDir
- python-3.x - 根据python中的列合并两个数据框
- centos - 如何在 centos 中更改/升级我的 GLIBCXX
- linux - 反转多个 SED 命令
- javascript - 使用 Javascript 修改页面上的一些 href 链接
- wpf - WPF C#如何通过代码创建派生样式
- r - 如何解决 R 代码中的单词重音问题
- eclipse-plugin - 如何获取 IProgressMonitor 的实例?