I have a dictionary of lists, of different lengths.
An example:
{'Naldo': ['03bd0bf4', 'a2609621'],
'Nino': ['03dc5b1f'],
'Matty Daly': ['03e48abb']}
I'm wondering the best way to save and then re-read this dictionary from disk.
Many thanks.
CodePudding user response:
I think you can make your exercise with pickle.
import pickle
tomp_dic = {'Naldo': ['03bd0bf4', 'a2609621'],
'Nino': ['03dc5b1f'],
'Matty Daly': ['03e48abb']}
tomp_file = open('dic.pckl', 'wb')
pickle.dump(tomp_dic, tomp_file)
tomp_file.close()
tomp_new_file = open('dic.pckl','rb')
tomp_new_dic = pickle.load(tomp_new_file)
print (tomp_new_dic)
CodePudding user response:
- save with json.dump
import json
data = {'Naldo': ['03bd0bf4', 'a2609621'],
'Nino': ['03dc5b1f'],
'Matty Daly': ['03e48abb']}
with open("./data.json", "w") as f:
json.dump(data, f, indent=4)
- load with json.load
import json
with open("./data.json", "r") as f:
data2 = json.load(data)