Mega Code Archive

 
Categories / Python / Database
 

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()