用lambda

时间:2016-02-24 13:48:17

标签: python pickle

我正在尝试挑剔defaultdict(),但却无法做到。什么是正确的方法?

pos = defaultdict(lambda: 0)
neg = defaultdict(lambda: 0)    

countdata = self.getCountdata(pos, neg, totals)
cPickle.dump(countdata, open(CDATA_FILE, 'w'))

这给出了:

Traceback (most recent call last):
  File "sentiment_worker.py", line 146, in <module>
    MyDict().gearman_worker.work()
  File "sentiment_worker.py", line 28, in __init__
    self.train()
  File "sentiment_worker.py", line 91, in train
    cPickle.dump(countdata, open(CDATA_FILE, 'w'))
  File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle function objects

当我尝试莳萝时:

import dill
dill.dumps(countdata, open(CDATA_FILE, 'w') )

给出:

 File "sentiment_worker.py", line 151, in <module>
    if __name__ == '__main__':
  File "sentiment_worker.py", line 29, in __init__
    self.train()
  File "sentiment_worker.py", line 96, in train
    # cPickle.dump(countdata, open(CDATA_FILE, 'w'))
  File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 243, in dumps
    dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
  File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 204, in dump
    pik = Pickler(file, protocol)
  File "/usr/local/lib/python2.7/dist-packages/dill/dill.py", line 387, in __init__
    StockPickler.__init__(self, *args, **kwds)
  File "/usr/lib/python2.7/pickle.py", line 202, in __init__
    raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
ValueError: pickle protocol must be <= 2

挑选和取消内容的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以使用dill执行此操作。你犯了一个错字...如果你想dill.dump一个文件,你应该使用dill.dumps而不是dump。如果您想dump为字符串,请使用dumps

>>> import dill
>>> from collections import defaultdict
>>> pos = defaultdict(lambda: 0)
>>> neg = defaultdict(lambda: 0)
>>> countdata = (pos,neg)
>>> _countdata = dill.loads(dill.dumps(countdata))
>>> _countdata
(defaultdict(<function <lambda> at 0x10917f7d0>, {}), defaultdict(<function <lambda> at 0x10917f8c0>, {}))
>>>
>>> # now dump countdata to a file 
>>> with open('data.pkl', 'wb') as f:
...     dill.dump(countdata, f)
...
>>>