本文主要是介绍Python3 Python对象持久化(pickle / shelve),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Python3 Python对象持久化(pickle / shelve)
本文由 Luzhuo 编写,转发请保留该信息.
原文: http://blog.csdn.net/rozol/article/details/71081854
以下代码以Python3.6.1为例
Less is more!
pickle
#coding=utf-8
# pickledemo.py Pickle
# 用于对Python对象进行序列化和反序列化的二进制协议import pickledef demo():# --- 序列化 ---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) # 从文件反序列化
这篇关于Python3 Python对象持久化(pickle / shelve)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!