2017-10-30 8 views
0

So habe ich ein einfaches Programm, das Daten in JSON-Datei legt. Das Problem, das ich habe, ist, dass die Datei zurückgesetzt wird, wenn ich das Programm neu starte und hinzufüge. Wie speichere ich die Datei, so dass das nächste Mal, wenn ich das Programm ausführe, nur an die vorherigen Daten angehängt wird. Tut mir leid, ein wenig zu Python neu. hier ist mein Code:JSON-Datei nicht speichern meine bisherigen Daten

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) 

Antwort

1

Anstelle von "w" Verwendung "a" (anhänge) Modus mit offener Funktion:

open("test.json", "a") as f: 
Verwandte Themen