Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
json文件未保存我以前的数据_Json_Save_Append_Dump - Fatal编程技术网

json文件未保存我以前的数据

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

所以我有一个简单的程序将数据放入json文件。我遇到的问题是,当我重新启动程序并添加到其中时,文件会重置。如何保存该文件,以便下次运行该程序时,它只会附加到以前的数据中。抱歉,python有点陌生。这是我的密码:

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: