Reputation: 87
I would like to make this very clear so ill start off with the case i know so everyone knows exactly what i mean. When i'm storing just a single dictionary in a pickle file this is the method i use and it works great.
#open pickle file
with open("a_pickle_file", 'rb') as input:
x = pickle.load(input)
#Now update the dict in your pickle file
a_dict.update(x)
Assume some arbitrary manipulation to this dictionary now i save like so:
#Save whatever u added to the dict
pickle.dump(open(a_dict,"a_pickle_file"))
Now moving on to the case that im unsure about and which seems to have little to no documentation from what i've found anyway. I want to do the same exact thing as above only this time i will be storing two dictionaries inside a list and then storing the list in the pickle file like so,
#Two dict
MyFirstDict = { 1: 'a' }
MySecondDict = { 2:'b' }
#Store them in a list
two_dict = [MyFirstDict, MySecondDict]
#save them
pkl.dump( two_dict, open( "two_dict_pkl_file", "wb" ) )
Now i have a two dictionaries inside a list and its stored in my pickle file. Now from here how does one load this pickle file for manipulation? Once it is loaded how can i access each dictionary inside for updating and manipulation. Finally, can i just use the same pkl.dump
statement above to re-save it. Thanks
EDIT
So for the most part the procedure is generally the same when doing this except for the part where you need to update your dictionaries with previous info. This is the output of a list of two dict in one pkl file:
[{'I': 'D', 'x': 'k', 'k': [datetime.time(11, 52, 3, 514000)]}, {'I': 'D', 'x': 'k', 'D': [datetime.time(11, 52, 3, 514000)]}]
As you can see it is stored oddly, and i can't seem to properly access each dict separately for updating their must be some syntax to properly do this.
Upvotes: 0
Views: 2354
Reputation: 77347
Just give it a try! Do pickle.load() and you'll see a list with two dicts in it. You can reference them in several ways, but this is a common way to do it:
MyFirstDict, MySecondDict = pickle.load(open("two_dict_pkl_file", "rb"))
And yes, you can just pickle.dump them again if you want to overwrite the existing contents.
EDIT
Here is a script illustrating the concept:
import pickle, datetime
test_file = "two_dict_pkl_file"
# store two dicts in a list
MyFirstDict = { 1: 'a' }
MySecondDict = { 2:'b' }
two_dict = [MyFirstDict, MySecondDict]
print "first pickle test"
print "this python list gets pickled:"
print two_dict
print
print "this is the pickle file"
pickle.dump( two_dict, open( test_file, "wb" ) )
print open(test_file, "rb").read()
print
print "update pickle test"
pickle_list = pickle.load(open(test_file, "rb"))
print "this python object is read back:"
print pickle_list
print
d1, d2 = pickle_list
two_dict = [d1, d2]
d1['time'] = d2['time'] = datetime.datetime.now().time()
pickle.dump( two_dict, open( test_file, "wb" ) )
print "this is the updated pickle:"
print open(test_file, "rb").read()
print
print "and this is the updated python:"
print pickle.load(open(test_file, "rb"))
...and its output
first pickle test
this python list gets pickled:
[{1: 'a'}, {2: 'b'}]
this is the pickle file
(lp0
(dp1
I1
S'a'
p2
sa(dp3
I2
S'b'
p4
sa.
update pickle test
this python object is read back:
[{1: 'a'}, {2: 'b'}]
this is the updated pickle:
(lp0
(dp1
I1
S'a'
p2
sS'time'
p3
cdatetime
time
p4
(S'\n2;\x02s\x95'
p5
tp6
Rp7
sa(dp8
I2
S'b'
p9
sg3
g7
sa.
and this is the updated python:
[{1: 'a', 'time': datetime.time(10, 50, 59, 160661)}, {2: 'b', 'time': datetime.time(10, 50, 59, 160661)}]
Upvotes: 1