Python Pickle 帮助
我不确定为什么这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要为此使用泡菜。使用数据库。
Python
dbm
模块 似乎完全符合你想要的。它为您提供了一个类似字典的界面,但数据保存到磁盘上。用法示例:
明天您可以加载数据:
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:
Tomorrow you can load the data:
我开始编辑您的代码以提高可读性,并在此过程中提取出
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 ownload
. 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 thanappend_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.