python - unpickling 命名元组时出错

标签 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())

在这里调用并 pickle :

     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 都会给出类似的错误。

最佳答案

我猜测您在一个模块中定义了 Agree,并尝试在未定义 Agree 的不同模块中加载数据。尝试如下所示的操作,如果有效,则将定义的命名元组导入到从中加载数据的模块中。

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

关于python - unpickling 命名元组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18683919/

相关文章:

Python正则表达式搜索并替换所有出现的地方

python - 在 Python 中将数字四舍五入到 5 万

python - 为什么将 pickle 文件加载到内存中会占用更多空间?

python - 在 Python 字典中找不到键时会引发什么异常?

python - 在 django CreateView 中将 args 和 kwargs 传递给具有额外内容的父类

database - sqlite接口(interface)错误: Error binding parameter 0 - probably unsupported type

python-2.7 - Django - 'datetime.date' 对象没有属性 'tzinfo'

c++ - 将 PicklingTools 与 QT Creator 一起使用

python - 具有 namedtuple 的多处理对象 - pickle 错误

python - Select2 和 django_filters 不查询外键