python3.X中pickle类的用法(cPickle模块移除了)

1、python3.x中移除了cPickle模块,可以使用pickle模块代替。最终我们将会有一个透明高效的模块。

2、因为存储的是对象,必须使用二进制形式写进文件

#!/usr/bin/python
# Filename: pickling.py

import pickle as p
#import pickle as p

shoplistfile = 'shoplist.data'
# the name of the file where we willl store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = open(shoplistfile, 'wb') #二进制打开
p.dump(shoplist, f)
f.close()

del shoplist # remove the shoplist

# Read back from the storage
f = open(shoplistfile, 'rb')
storedlist = p.load(f)
print(storedlist)


你可能感兴趣的:(Python,python,pickle)