代碼如下:
"""
儲存器
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報錯