Python 泡菜和搁置有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4103430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between pickle and shelve?
提问by zubinmehta
I am learning about object serialization for the first time. I tried reading and 'googling' for differences in the modules pickle and shelve but I am not sure I understand it. When to use which one? Pickle can turn every python object into stream of bytes which can be persisted into a file. Then why do we need the module shelve? Isn't pickle faster?
我是第一次学习对象序列化。我尝试阅读和“谷歌搜索”以了解模块 pickle 和 shelve 的差异,但我不确定我是否理解。什么时候用哪一种?Pickle 可以将每个 python 对象转换为可以持久化到文件中的字节流。那我们为什么需要模块搁架呢?泡菜不是更快吗?
采纳答案by wkl
pickleis for serializing some object (or objects) as a single bytestream in a file.
pickle用于将某个(或多个)对象序列化为文件中的单个字节流。
shelvebuilds on top of pickleand implements a serialization dictionary where objects are pickled, but associated with a key (some string), so you can load your shelved data file and access your pickled objects via keys. This could be more convenient were you to be serializing many objects.
shelve构建pickle并实现了一个序列化字典,其中对象被腌制,但与键(某些字符串)相关联,因此您可以加载搁置的数据文件并通过键访问您的腌制对象。如果您要序列化许多对象,这可能会更方便。
Here is an example of usage between the two. (should work in latest versions of Python 2.7 and Python 3.x).
这是两者之间的用法示例。(应该适用于最新版本的 Python 2.7 和 Python 3.x)。
pickleExample
pickle例子
import pickle
integers = [1, 2, 3, 4, 5]
with open('pickle-example.p', 'wb') as pfile:
pickle.dump(integers, pfile)
This will dump the integerslist to a binary file called pickle-example.p.
这会将integers列表转储到名为pickle-example.p.
Now try reading the pickled file back.
现在尝试读取腌制文件。
import pickle
with open('pickle-example.p', 'rb') as pfile:
integers = pickle.load(pfile)
print integers
The above should output [1, 2, 3, 4, 5].
以上应该输出[1, 2, 3, 4, 5].
shelveExample
shelve例子
import shelve
integers = [1, 2, 3, 4, 5]
# If you're using Python 2.7, import contextlib and use
# the line:
# with contextlib.closing(shelve.open('shelf-example', 'c')) as shelf:
with shelve.open('shelf-example', 'c') as shelf:
shelf['ints'] = integers
Notice how you add objects to the shelf via dictionary-like access.
请注意您如何通过类似字典的访问将对象添加到架子上。
Read the object back in with code like the following:
使用如下代码读回对象:
import shelve
# If you're using Python 2.7, import contextlib and use
# the line:
# with contextlib.closing(shelve.open('shelf-example', 'r')) as shelf:
with shelve.open('shelf-example', 'r') as shelf:
for key in shelf.keys():
print(repr(key), repr(shelf[key]))
The output will be 'ints', [1, 2, 3, 4, 5].
输出将为'ints', [1, 2, 3, 4, 5].
回答by as - if
According to pickledocumentation:
根据泡菜文档:
Serialization is a more primitive notion than persistence; although picklereads and writes file objects, it does not handle the issue of naming persistent objects, nor the (even more complicated) issue of concurrent access to persistent objects. The picklemodule can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. Perhaps the most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The shelvemodule provides a simple interface to pickle and unpickle objects on DBM-style database files.
序列化是一个比持久化更原始的概念;虽然pickle读写文件对象,但它没有处理命名持久对象的问题,也没有处理持久对象并发访问(甚至更复杂)的问题。所述泡菜模块可以将一个复杂的对象成字节流,它可以改造字节流成一个对象具有相同的内部结构。也许对这些字节流做的最明显的事情是将它们写入文件,但也可以通过网络发送它们或将它们存储在数据库中。该货架模块提供了一个简单的界面,咸菜和unpickle对象上DBM风格的数据库文件。

