json文件未保存我以前的数据
所以我有一个简单的程序将数据放入json文件。我遇到的问题是,当我重新启动程序并添加到其中时,文件会重置。如何保存该文件,以便下次运行该程序时,它只会附加到以前的数据中。抱歉,python有点陌生。这是我的密码:json文件未保存我以前的数据,json,save,append,dump,Json,Save,Append,Dump,所以我有一个简单的程序将数据放入json文件。我遇到的问题是,当我重新启动程序并添加到其中时,文件会重置。如何保存该文件,以便下次运行该程序时,它只会附加到以前的数据中。抱歉,python有点陌生。这是我的密码: import json from collections import defaultdict vocabulary = defaultdict(list) def update_vocabulary(category, value): vocabulary[categor
import json
from collections import defaultdict
vocabulary = defaultdict(list)
def update_vocabulary(category, value):
vocabulary[category].append(value)
with open("test.json", "w") as f:
json.dump(vocabulary, f, indent = 2)
while 1:
input_category = input("give me a category ")
input_value = input("give me a value for that category ")
update_vocabulary(input_category, input_value)
使用带有open函数的追加模式代替w:
open("test.json", "a") as f: