Sto usando Python e ho un file JSON in cui vorrei aggiornare un valore relativo a una determinata chiave. Cioè, ho la my_file.json
contenente i seguenti datiCome aggiornare un file JSON usando Python?
{"a": "1", "b": "2", "c": "3"}
e vorrei cambiare solo il valore relativo alla chiave b
2
-9
in modo che il file aggiornato sguardo come come:
{"a": "1", "b": "9", "c": "3"}
Come posso farlo?
Ho provato quanto segue ma senza successo (le modifiche non vengono salvate nel file):
with open('my_file.json', 'r+') as f:
json_data = json.load(f)
json_data['b'] = "9"
f.close()
Questo può aiutare: http://stackoverflow.com/questions/21035762/python-read -json-file-and-modify – Vipul