#小练习 pickle模块_分别存储python对象 分类: python Module 2013-09-05 18:07 379人阅读 评论(0) 收藏
如果使用pickle.dump()方法分别存储n个python对象,则读取时也需要n个pickle.load(file)的方法,才能将python对象全部读取出来。
import pickle as p,pprint
shoplistfile =r'G:\data.pkl'
#objects
shoplist = ['apple','mango','carrot']
shoptuple = ('fish',1,'chicken',2)
f = open(shoplistfile,'wb')
#存储对象 分别存储python对象
p.dump(shoplist,f)
p.dump(shoptuple,f,-1)
f.close()
#remove the object
##del shoplist
#读取对象 分别读取python对象
f = open(shoplistfile,'r')
data1 = p.load(f)
pprint.pprint(data1)
data2 = p.load(f)
print data2
版权声明:本文为博主原创文章,未经博主允许不得转载。