pickle - python 序列化对象

python 序列化对象示例

# 导入 pickle 包
import pickle

# 打开二进制文件用于写入序列化对象
f = open('/home/mark/xx.pk', 'wb')
for i in [1,2,3,4,5]:
    pickle.dump(i, f)
f.close()

# 从文件中反序列化出对象
f = open('/home/mark/xx.pk', 'rb')
while True:
    try:
        v = pickle.load(f)
        print(v)
    except Exception as e:
        break

你可能感兴趣的:(pickle - python 序列化对象)