Demonstrates pickling and shelving data : Pickle « Database « Python






Demonstrates pickling and shelving data

 

import cPickle, shelve

print "Pickling lists."
variety = ["A", "B", "C"]
shape = ["a", "b", "c"]
brand = ["1", "2", "3"]
f = open("pickles1.dat", "w")
cPickle.dump(variety, f)
cPickle.dump(shape, f)
cPickle.dump(brand, f)
f.close()


f = open("pickles1.dat", "r")
variety = cPickle.load(f)
shape = cPickle.load(f)
brand = cPickle.load(f)
print variety, "\n", shape, "\n", brand
f.close()

pickles = shelve.open("pickles2.dat")
pickles["variety"] = ["A", "B", "C"]
pickles ["shape"] = ["a", "b", "c"]
pickles["brand"] = ["1", "2", "3"]
pickles.sync()

for key in pickles.keys():
    print key, "-", pickles[key]
pickles.close()

   
  








Related examples in the same category

1.pickling and shelving datapickling and shelving data
2.pickle Module
3.Use Pickle to save and read
4. unpickle the object