本文介绍了python 3中pickle和_pickle有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,想要实现快速对象序列化.我尝试使用json,但是太慢了,也尝试使用marshall模块,但是marshall序列化的对象大小是pickle的6-7倍,所以我决定在我的项目中使用pickle.我读了关于 cPickle 模块的文章,读得很快,但是在 python 3 中没有这样的模块,文档说名为 _pickle 的模块是用 C 编写的.所以在我的项目中我使用

I am new in python and want implement fast object serialization. I was trying to use json, but it was too slow, also was trying to use marshall module, but the size of the objects serialized by marshall 6-7 times more than pickle, so i decided to use pickle in my project. I read about cPickle module, read that it quite fast, but there is no such module in python 3 and docs says that module named _pickle is written in C. So in my projects i use

import _pickle as pickle

pickle 和 _pickle 有什么区别吗?如何实现更快的对象序列化/反序列化?

Is any difference between pickle and _pickle? How i can implement faster objects serialization/deserialization?

推荐答案

pickle 模块 已经 导入 _pickle(如果可用).它是 pickle 模块的 C 优化版本,透明使用.

The pickle module already imports _pickle if available. It is the C-optimized version of the pickle module, and is used transparently.

来自 pickle.py代码>源代码:

From the pickle.py source code:

# Use the faster _pickle if possible
try:
    from _pickle import *
except ImportError:
    Pickler, Unpickler = _Pickler, _Unpickler

以及来自 pickle 模块文档:

and from the pickle module documentation:

pickle 模块有一个用 C 编写的透明优化器 (_pickle).只要可用就使用它.否则使用纯 Python 实现.

在 Python 2 中,_pickle 被称为 cPickle,但已更新以允许透明使用作为实现细节.

In Python 2, _pickle was known as cPickle, but has been updated to allow the transparent use as an implementation detail.

这篇关于python 3中pickle和_pickle有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:57