我不知道爲什麼這個Pickle示例沒有顯示兩個字典定義。據我瞭解,「ab +」應該表示pickle.dat文件被附加到並可以從中讀取。我對整個泡菜概念並不熟悉,但網絡上的教程似乎並不僅限於初始存儲。Python Pickle幫助
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。
這是怎麼回事?
不要雙擊空格你的代碼。而是用四個空格縮進。 – 2010-08-04 16:32:43