泡菜和货架有什么区别?

发布于 2024-09-30 16:50:02 字数 193 浏览 15 评论 0原文

什么时候适合使用pickle,什么时候适合使用shelve?也就是说,他们的做法有何不同?

根据我的研究,我了解到 pickle 可以将每个 Python 对象转换为可以保存到文件中的字节流。那为什么我们还需要shelve呢? pickle 不是更快吗?

When is it appropriate to use pickle, and when is it appropriate to use shelve? That is to say, what do they do differently from each other?

From my research, I understood that pickle can turn every Python object into stream of bytes which can be persisted into a file. Then why do we need shelve as well? Isn't pickle faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-10-07 16:50:02

pickle 用于将某些对象(或多个对象)序列化为文件中的单个字节流。

shelve 构建在 pickle 之上,并实现一个序列化字典,其中对象被腌制,但与键(某个字符串)相关联,因此您可以加载搁置的数据文件并访问通过按键腌制的对象。如果您要序列化许多对象,这可能会更方便。

这是两者之间的用法示例。 (应该适用于最新版本的 Python 2.7 和 Python 3.x)。

pickle 示例

import pickle

integers = [1, 2, 3, 4, 5]

with open('pickle-example.p', 'wb') as pfile:
    pickle.dump(integers, pfile)

这会将integers 列表转储到名为pickle-example.p 的二进制文件中。

现在尝试读回腌制的文件。

import pickle

with open('pickle-example.p', 'rb') as pfile:
    integers = pickle.load(pfile)
    print(integers)

上面应该输出 [1, 2, 3, 4, 5]

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

请注意如何通过类似字典的访问将对象添加到架子中。

使用如下代码读回对象:

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]))

输出将为 'ints', [1, 2, 3, 4, 5]

pickle is for serializing some object (or objects) as a single bytestream in a file.

shelve builds on top of pickle and 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.

Here is an example of usage between the two. (should work in latest versions of Python 2.7 and Python 3.x).

pickle Example

import pickle

integers = [1, 2, 3, 4, 5]

with open('pickle-example.p', 'wb') as pfile:
    pickle.dump(integers, pfile)

This will dump the integers list to a binary file called 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].

shelve Example

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].

绝不放开 2024-10-07 16:50:02

根据 pickle 文档:

序列化是一个比持久化更原始的概念;尽管pickle读取和写入文件对象,但它不处理命名持久对象的问题,也不处理对持久对象的并发访问(甚至更复杂)的问题。 pickle模块可以将复杂的对象转换为字节流,并且可以将字节流转换为具有相同内部结构的对象。也许处理这些字节流最明显的事情是将它们写入文件,但也可以想象通过网络发送它们或将它们存储在数据库中。 shelve 模块提供了一个简单的接口来 pickle 和 unpickle DBM 样式数据库文件上的对象。

According to pickle documentation:

Serialization is a more primitive notion than persistence; although pickle reads 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 pickle module 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 shelve module provides a simple interface to pickle and unpickle objects on DBM-style database files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文