我有一個小問題!如何將對象或字符串添加到 Python 中已有的 JSON 文件中?目前我已經寫了那段代碼。但它總是會添加一個新的 JSON 文件嗎?我希望你知道我的問題。:Djson.dump(txtdictionary_main,open('filename-manager.json','w'),indent=4,sort_keys=True)
1 回答

BIG陽
TA貢獻1859條經驗 獲得超6個贊
將文件轉換為字典:
with open('filename-manager.json') as json_file: txtdictionary_main = json.load(json_file)
將值添加到字典中:
txtdictionary_main[key] = value
將字典轉換回 JSON:
json_object = json.dumps(txtdictionary_main , indent = 4)
添加回答
舉報
0/150
提交
取消