Python3-笔记-E-007-库-序列号pickle

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()
posted @ 2017-10-26 11:36  魏桐  阅读(224)  评论(0编辑  收藏  举报