unpickling namedtuple时出错

时间:2013-09-08 12:55:43

标签: python python-2.7 pickle namedtuple

全局变量Agree是在所有函数之外定义的命名元组:

Agree = collections.namedtuple('Agree', ['kappa', 'alpha','avg_ao'], verbose=True)

从此函数返回指定的元组:

def getagreement(task):
    return Agree(kappa=task.kappa(),alpha=task.alpha(),avg_ao=task.avg_Ao())

在这里被称为腌制:

     future_dict[executor.submit(getagreement,task)]=frozenset(annotators)
       ... 
       detaildata[future_dict[future]]=future.result()

 cPickle.dump(detaildata,open(os.path.dirname(jsonflist[0])+'\\out.picl','w'))

Unpickling会出错:

c=cPickle.load(open(subsdir))
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  AttributeError: 'module' object has no attribute 'Agree'

反汇编文件:

 pickletools.dis(f)
  126: c    GLOBAL     '__builtin__ tuple'
  147: p    PUT        9
  151: (    MARK
  152: F        FLOAT      0.22320438764335693
  174: F        FLOAT      0.21768346003098427
  196: F        FLOAT      0.7004133685136325
  218: t        TUPLE      (MARK at 151)
  219: t    TUPLE      no MARK exists on stack
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\Lib\pickletools.py", line 2009, in dis
    raise ValueError(errormsg)
ValueError: no MARK exists on stack

pickle和cPickle都会出现类似错误。

1 个答案:

答案 0 :(得分:1)

我猜你在一个模块中定义了Agree,并尝试在不定义Agree的不同模块中加载数据。尝试类似下面的内容,如果它工作,则将定义的名为tuple的导入定义到加载数据的模块中。

import collections
import cPickle
Agree = collections.namedtuple('Agree', ['kappa', 'alpha','avg_ao'], verbose=True)
c = cPickle.load(open(subsdir))