python的存储

来源:互联网 发布:阿里云免费邮箱入口 编辑:程序博客网 时间:2024/05/23 11:52

代码:

#!/usr/bin/python# Filename: pickling.pyimport cPickle as p#import pickle as pshoplistfile = 'shoplist.data'# the name of the file where we will store the objectshoplist = ['apple', 'mango', 'carrot']# Write to the filef = file(shoplistfile, 'w')p.dump(shoplist, f) # dump the object to a filef.close()del shoplist # remove the shoplist# Read back from the storagef = file(shoplistfile)storedlist = p.load(f)print storedlist

程序运行结果:

['apple', 'mango', 'carrot']


shoplist.data的内容:

(lp1S'apple'p2aS'mango'p3aS'carrot'p4a.

dump()出来的文件还是要用load()来读比较好。