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!

7th May 2019, 5:02 PM
Βαγγέλης Τζουβάρας
Βαγγέλης Τζουβάρας - avatar
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)
7th May 2019, 6:13 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
Thanks for the answers both of them are amazing!! I followed anna's way and works like a charm thanks :D
7th May 2019, 7:57 PM
Βαγγέλης Τζουβάρας
Βαγγέλης Τζουβάρας - avatar
+ 3
https://code.sololearn.com/c45QpP464usn/?ref=app i made this. I hope this is a nice use :D
7th May 2019, 8:22 PM
Βαγγέλης Τζουβάρας
Βαγγέλης Τζουβάρας - avatar