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)


相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2021-05-20
  • 2021-06-08
  • 2021-12-08
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
猜你喜欢
  • 2021-12-02
  • 2021-08-16
  • 2021-06-13
  • 2021-07-09
  • 2021-10-07
  • 2022-12-23
  • 2021-09-11
相关资源
相似解决方案