您的位置:首页 > 编程语言 > Python开发

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: