0
How to import and edit a dictionary from txt file?
I want to know if there is anyway to import a dictionary from a text file save it close it. And then open it again and edit the entries like: {"A":1,"b":2} ( first entries ) Open file {"A":1,"b":2} . append("C":3) save it and use it again etc. Thanks for advance. Best Regards!
4 Réponses
+ 6
import json
d = {'a': 1, 'b': 2}
with open('file.json', 'w') as f:
json.dump(d, f)
with open('file.json', 'r') as f:
d = json.load(f)
d['c'] = 3
print(d)
+ 3
Thanks for the answers both of them are amazing!! I followed anna's way and works like a charm thanks :D
+ 3
https://code.sololearn.com/c45QpP464usn/?ref=app i made this. I hope this is a nice use :D