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()
报错:TypeError: must be str, not bytes
原因:上面的用法是python 2的用法
python 3中,要使用pickle时,打开文件需要使用 wb rb