python pickle
2016-01-27 13:57
471 查看
Python's standard way of saving class instances and reloading them is pickle mechanism. A limitation of pickle is that it does not save the code or data of a class with instance of the class being serialized.
The two module pickle and cPickle have the same functionalities, but cPickle, coded in C, is much faster
import cPickle
f = file('obj.save', 'wb')
cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()
if you want your saved object to be stored efficiently, don't forget to use cPickle.HIGHEST_PROTOCOL. The resulting file can be dozens of times smaller than with the default protocol
Opening your file in binary mode('b') is required for portability(especially between Unix and Windows)
The two module pickle and cPickle have the same functionalities, but cPickle, coded in C, is much faster
import cPickle
f = file('obj.save', 'wb')
cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()
if you want your saved object to be stored efficiently, don't forget to use cPickle.HIGHEST_PROTOCOL. The resulting file can be dozens of times smaller than with the default protocol
Opening your file in binary mode('b') is required for portability(especially between Unix and Windows)
相关文章推荐
- python系列之 - 装饰器
- python中的比较
- Python学习之--数据基础(二)
- python 输出表达式之format
- Python Gzip压缩与解压模块
- Python前世今生
- 关于python中的flush问题
- python下载地址
- Python 7.2 StringIO和BytesIO
- Python图像灰度变换及图像数组操作
- python模块:win32com用法详解
- Head First Python(如何向PYPI发布你的代码)学习笔记
- Python 高级特性 : 切片
- webdriver的初步学习(IE,Chrome,Firefox)
- python测试框架及应用
- python的urllib和urllib2
- 我的Python成长之路---第四天---Python基础(16)---2016年1月23日(寒风刺骨)
- Python的学习资源
- 【python】列表实例1
- python-类和对象(属性、方法)的动态绑定