Python3.+中使用Cpickle

代码如下:

"""
储存器
cpickle
"""
import _pickle as c_pickle

shop_list_data = "shop_list.pkl"
shop_list = ["Desk", "Phone", "Juice"]
file = open(shop_list_data, "wb")  # 此处要写成wb,否则会报 write() argument must be str, not bytes报错
c_pickle.dump(shop_list, file, 0)  # 第三个参数表示以ACII的模式进行储存,否则会乱码
file.close()  # 一定要close 否则在下面load的时候会报Ran out of input的错误

del shop_list

my_list = open(shop_list_data, "rb")  # 此处要写成rb,否则会报 write() argument must be str, not bytes报错
print("gggg----", my_list)
hi_list = c_pickle.load(my_list)
print("取出来的数据------", hi_list)

其中需要注意的点
1,python3.0+要这样引用

import _pickle as c_pickle

2,

file = open(shop_list_data, "wb")  # 此处要写成wb,否则会报 write() argument must be str, not bytes报错

3,

c_pickle.dump(shop_list, file, 0)  # 第三个参数表示以ACII的模式进行储存,否则会乱码

4,

file.close()  # 一定要close 否则在下面load的时候会报Ran out of input的错误

5,

my_list = open(shop_list_data, "rb")  # 此处要写成rb,否则会报 write() argument must be str, not bytes报错

你可能感兴趣的:(Python3.+中使用Cpickle)