Python Pickle 帮助

发布于 2024-09-13 01:38:51 字数 733 浏览 6 评论 0原文

我不确定为什么这个 Pickle 示例没有显示两个字典定义。据我了解,“ab+”应该意味着 pickle.dat 文件被附加到并且可以从中读取。我对整个泡菜概念很陌生,但网上的教程似乎并没有超出初始存储的范围。

import cPickle as pickle

def append_object(d, fname):
    """appends a pickle dump of d to fname"""
    print "append_hash", d, fname
    with open(fname, 'ab') as pickler:
        pickle.dump(d, pickler)

db_file = 'pickle.dat'

cartoon = {}
cartoon['Mouse'] = 'Mickey'
append_object(cartoon, db_file)

cartoon = {}
cartoon['Bird'] = 'Tweety'
append_object(cartoon, db_file)

print 'loading from pickler'
with open(db_file, 'rb') as pickler:
    cartoon = pickle.load(pickler)
print 'loaded', cartoon

理想情况下,我希望使用 for 循环构建一个字典,然后将键:值对添加到 pickle.dat 文件中,然后清除字典以节省一些 RAM。

这是怎么回事?

I'm not sure why this Pickle example is not showing both of the dictionary definitions. As I understand it, "ab+" should mean that the pickle.dat file is being appended to and can be read from. I'm new to the whole pickle concept, but the tutorials on the net don't seem to go beyond just the initial storage.

import cPickle as pickle

def append_object(d, fname):
    """appends a pickle dump of d to fname"""
    print "append_hash", d, fname
    with open(fname, 'ab') as pickler:
        pickle.dump(d, pickler)

db_file = 'pickle.dat'

cartoon = {}
cartoon['Mouse'] = 'Mickey'
append_object(cartoon, db_file)

cartoon = {}
cartoon['Bird'] = 'Tweety'
append_object(cartoon, db_file)

print 'loading from pickler'
with open(db_file, 'rb') as pickler:
    cartoon = pickle.load(pickler)
print 'loaded', cartoon

Ideally, I was hoping to build up a dictionary using a for loop and then add the key:value pair to the pickle.dat file, then clear the dictionary to save some RAM.

What's going on here?

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

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

发布评论

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

评论(2

骄傲 2024-09-20 01:38:51

不要为此使用泡菜。使用数据库。

Python dbm 模块 似乎完全符合你想要的。它为您提供了一个类似字典的界面,但数据保存到磁盘上。

用法示例:

>>> import dbm
>>> x = dbm.open('/tmp/foo.dat', 'c')
>>> x['Mouse'] = 'Mickey'
>>> x['Bird'] = 'Tweety'

明天您可以加载数据:

>>> import dbm
>>> x = dbm.open('/tmp/foo.dat', 'c')
>>> print x['Mouse']
Mickey
>>> print x['Bird']
Tweety

Don't use pickle for that. Use a database.

Python dbm module seems to fit what you want perfectly. It presents you with a dictionary-like interface, but data is saved to disk.

Example usage:

>>> import dbm
>>> x = dbm.open('/tmp/foo.dat', 'c')
>>> x['Mouse'] = 'Mickey'
>>> x['Bird'] = 'Tweety'

Tomorrow you can load the data:

>>> import dbm
>>> x = dbm.open('/tmp/foo.dat', 'c')
>>> print x['Mouse']
Mickey
>>> print x['Bird']
Tweety
韬韬不绝 2024-09-20 01:38:51

我开始编辑您的代码以提高可读性,并在此过程中提取出 append_object

这里有很多困惑。首先,pickle.dump 完整地写入了一个 Python 对象。您可以将多个对象放入一个 pickle 文件中,但每个对象都需要自己的加载。该代码执行了您的要求,并加载了您写入文件的第一个字典。第二个字典在那里等待读取,但它不是与第一个字典的串联,它是它自己的可加载字典。

不要低估名字的重要性。 append_object 不是一个好名字,但它与 append_to_object 不同。

如果您要打开文件进行读取,只需打开它进行读取即可,写入或追加时也是如此。它不仅使您的意图更加清晰,而且可以防止愚蠢的错误。

I started to edit your code for readability and factored out append_object in the process.

There are multiple confusions here. The first, is that pickle.dump writes a Python object in its entirety. You can put multiple objects in a pickle file, but each needs its own load. The code did what you asked of it and loaded the first dictionary you wrote to the file. The second dictionary was there waiting to be read but it isn't a concatenation to the first, it is its own loadable.

Don't underestimate the importance of names. append_object isn't a great name, but it is different than append_to_object.

If you are opening a file for reading, just open it for reading and the same for writing or appending. Not only does it make your intentions more clear but it prevents silly errors.

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