#小练习 pickle模块 _ 一起存储python对象 分类: python Module 2013-09-05 18:11 399人阅读 评论(0) 收藏
重要内容:
将n个python对象放入一个元组中,一起写入文件,即pickle.dump((shoplist,shoptuple),f)。在读取时,使用多元赋值,读取数据,即:a,b=pickle.load(f)import pickle as p,pprint
shoplistfile =r'G:\data2.pkl'#objects
shoplist = ['apple','mango','carrot']
shoptuple = ('fish',1,'chicken',2)
f = open(shoplistfile,'wb')
#存储对象 以元组形式将python对象写入文件
p.dump((shoplist,shoptuple),f)
##p.dump(shoptuple,f)
f.close()
#remove the object
del shoplist
del shoptuple
#读取对象
f = open(shoplistfile,'rb')
a,b = p.load(f) #多元赋值
print a,b #分别读取python对象
print p.load(f)
#或使用 print p.load(f) 读取python对象元组,与(shoplist,shoptuple)格式一致。版权声明:本文为博主原创文章,未经博主允许不得转载。