I have a process which runs and creates three dictionaries: 2 rather small, and 1 large.
I know I can store one dictionary like:
import cPickle as pickle
with open(filename, 'wb') as fp:
pickle.dump(self.fitResults, fp)
What I'd like to do is store all 3 dictionaries in the same file, with the ability to load in the three dictionaries separately at another time. Something like
with open(filename, 'rb') as fp:
dict1, dict2, dict3 = pickle.load(fp)
Or even better just load the first two dictionaries, and make it optional whether to load the third (large) one. Is this possible or should I go about this in a completely different way?
I recommend the oft forgotten
shelve
module which effectively provides you with a persistent dictionary backed by Berkley DB file or dbm file (as selected byanydbm
). The db should provide performance improvements (for your big dictionary).Example usage:
As mentioned here, you can pickle several objects into the same file, and load them all (in the same order):
Then:
You can save your dictionaries in a specific order so that while loading them, you've the option to select only the preferred ones.
For e.g, if you store dictionaries in the order:
smallDict1
,smallDict2
,largeDict1
You can load only the small ones by setting appropriate range while loading
(Here
for i in range(2) ...
)Sure, you just dump each one separately and then load them separately:
make sure to dump the big on last so you can load the little ones without loading the big one first. I imagine you could even get clever and store the file positions where each dump starts in a header of sorts and then you could seek to that location before loading (but that's starting to get a little more complicated).