1. 程式人生 > >Python3-筆記-E-007-庫-序列號pickle

Python3-筆記-E-007-庫-序列號pickle

ring pic data -c sts font port mil 序列號

import pickle

# --- 序列化 ---用於對Python對象進行序列化和反序列化的二進制協議
f = open("pickle.txt", "wb+")
lists = [123, "中文", [456]]
strs = "字符串"
num = 123
# 寫入
pickle.dump(lists, f) # 序列化到文件
pickle.dump(strs, f)
pickle.dump(num, f)
# 關閉
f.close()
# --- 反序列化 ---
f = open("pickle.txt", "rb+")
# 讀取
data = pickle.load(f) # 從文件反序列化
print(data)
data = pickle.load(f)
print(data)
data = pickle.load(f)
print(data)
f.close()

Python3-筆記-E-007-庫-序列號pickle