python的存储
代码:#!/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', 'man
·
代码:
#!/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 object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist
程序运行结果:
['apple', 'mango', 'carrot']
shoplist.data的内容:
(lp1
S'apple'
p2
aS'mango'
p3
aS'carrot'
p4
a.
dump()出来的文件还是要用load()来读比较好。
更多推荐
所有评论(0)