pickle handling multiple objects ..

Peter Otten __peter__ at web.de
Sun Feb 26 07:04:00 EST 2012


Smiley 4321 wrote:

> If I have a sample python code to be executed on Linux. How should  I
> handle multiple objects with 'pickle' as below -
> 
> -------
> #!/usr/bin/python
> 
> import pickle
> 
> #my_list = {'a': 'Apple', 'b': 'Mango', 'c': 'Orange', 'd': 'Pineapple'}
> #my_list = ('Apple', 'Mango', 'Orange', 'Pineapple')
> my_list = ['Apple', 'Mango', 'Orange', 'Pineapple']
> #my_list = ()
> output = open('readfile.pkl', 'wb')
> pickle.dump(my_list, output)
> output.close()
> 
> my_file = open('readfile.pkl', 'rb')
> my_list2 = pickle.load(my_file)
> my_file.close()
> 
> print my_list
> print my_list2
> -----
> 
> This code works fine but now I have to handle multiple objects?

You never do that with pickle. You pack your data into a single object 
before you store it, and unpack it after loading it. Here's an example using 
a tuple as the container:

fruit = ["apple", "orange"]
vegetables = ["potato", "tomato"]
beverages = ["milk", "coffee", "water"]

with open("tmp.pickle", "wb") as f:
    pickle.dump((fruit, vegetables, beverages), f)

with open("tmp.pickle", "rb") as f:
    fruit, vegetables, beverages = pickle.load(f)

print fruit
print vegetables
print beverages

This is however a bit errorprone. If you accidentally write the loading code 
as 

fruit, beverages, vegetables = pickle.load(f)

you'll end up drinking potatoes. A better container would be a dict. 
Something like

pickle.dump(dict(fruit=fruit, vegetables=vegetables, beverages=beverages), 
f)
...
data = pickle.load(f)
fruit = data["fruit"]
beverages = data["beverages"]
vegetables = data["vegetables"]

is harder to get wrong. It is also easier to extend. Code that only reads 
the pickle will happily ignore extra keys in the dictionary. If you follow 
that path somewhat more you will probably drop the lowlevel pickle and use a 
key-value store like Python's shelve instead, see

http://docs.python.org/library/shelve.html





More information about the Python-list mailing list