dump python pickle_关于Python的pickle模块dump的[,protocol]协议问题?

google 一把就有

From the f="https://docs.python.org/2/library/pickle.html#data-stream-format">pickle module data format documentation:There are currently 3 different protocols which can be used for pickling.Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python.

Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.

Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes.

[...]If a protocol is not specified, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version available will be used.

Stick with protocol version 2, especially if you are using custom classes derived from object(new-style classes). Which most modern code does, these days.

Unless you need to maintain backwards compatibility with older Python versions, it's easiest to just stick with the highest protocol version you can lay your hands on:

with open("prot2", 'wb') as pfile:

pickle.dump(d, pfile, protocol=pickle.HIGHEST_PROTOCOL)

Because this is a binary format, make sure to use 'wb' as the file mode!

cPickle and pickle are mostly compatible; the differences lie in the API offered. For most use-cases, just stick with cPickle; it is faster. Quoting the documentation again:First, cPickle can be up to 1000 times faster than pickle because the former is implemented in C. Second, in the cPickle module the callables Pickler() and Unpickler() are functions, not classes. This means that you cannot use them to derive custom pickling and unpickling subclasses. Most applications have no need for this functionality and should benefit from the greatly improved performance of the cPickle module.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值