[Tutor] doubt: 2nd part

Hugo González Monteverde hugonz-lists at h-lab.net
Thu Mar 2 18:52:15 CET 2006


Hi Joaquin,

Remember to Reply-All for the whole list to receive the message.

Joaquin Sanchez Sanchez wrote:
> Im proving pickle in python.
> As I mentioned before, i do pickle.dump for two times,
> because I want to save two dictionaries.
> 
> Then to save the dictionaries, with one pickle.load()
> is not enough, because I only load the first
> dictionary.
You did dump() twice, now you need to do load() twice too.. load() does 
all of the work with the file, just call it again with the same file 
object, you will get the first object you pickled.

> 
> So, if I want to save the dictionaries in several
> sessions, How can I with one cpickle.load load all the file?

If you want to do it in one step, I can think of composing a tuple of 
the variables you want to pickle, like this:

pickle.dump( (mydict1, mydict2) , fileo)

Then you could load it like this:

mydict1, mydict2 = pickle.load(fileo)

===========
See, I just tested it:

 >>> import pickle
 >>> fileo = open('lala', 'w')
 >>> pickle.dump( ('this is some data', 'this is more data'), fileo)
 >>> fileo.close()
 >>>
 >>> fileo2 = open('lala', 'r')
 >>> pickle.load(fileo2)
('this is some data', 'this is more data')
 >>>
===========

Hugo


More information about the Tutor mailing list