您的
json_depred对象是Python字典;您只需在其中添加密钥,然后重新编码并重写文件即可:
import jsonwith open(json_file) as json_file: json_depred = json.load(json_file)json_depred['ADDED_KEY'] = 'ADDED_VALUE'with open(json_file, 'w') as json_file: json.dump(json_depred, json_file)
我在这里(带有
with语句)将打开的文件对象用作上下文管理器,因此Python完成后会自动关闭文件。



