This is an example of using pickle to store a dictionary, we then reopen this and compare it to the original.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| import pickle
items = {'apple': 23, 'cherry': 56, 'pear': 33}
with open('items.pickle', 'wb') as handle:
pickle.dump(items, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('items.pickle', 'rb') as handle:
items2 = pickle.load(handle)
print (items)
print (items2)
print (items == items2)
|
Returns:
1
2
3
| {'apple': 23, 'cherry': 56, 'pear': 33}
{'apple': 23, 'cherry': 56, 'pear': 33}
True
|