#!/usr/bin/python
#Filename:pickling.py
import cPickle as p
#import pickle as p
shoplistfile='shoplist.data'
#the name of the file where we will store the obj
shoplist=['apple','mango','carrot']
#write to the file
f=file(shoplistfile,'w')
p.dump(shoplist,f)#dump the obj to a file
f.close()
del shoplist #remove the shoplist
#Read back from the storage
f=file(shoplistfile)
storedlist=p.load(f)
print storedlist
先创建shoplist.data,然后向里面追加数据,最后读取
注意方法:
p.dump(shoplist,f)
p.load(f)