Demonstrates pickling and shelving data,picklingshelving,import cPick


import cPickle, shelveprint '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', brandf.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()

评论关闭