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()
转载于:https://www.cnblogs.com/vito13/p/7735970.html