- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将一些结果写入 pickle 文件,如下所示:
raw_X = (self.token_ques(text) for text in training_data)
with open('/root/Desktop/classifier_result.pkl', 'wb') as handle:
pickle.dump(raw_X, handle)
错误:
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle generator objects
任何帮助将不胜感激。
最佳答案
当您想要 pickle 数据时,不要使用生成器表达式。请改用列表理解,或在生成器上调用 list()
以捕获所有生成的元素以进行 pickle 。
例如,以下工作正常:
raw_X = [self.token_ques(text) for text in training_data]
with open('/root/Desktop/classifier_result.pkl', 'wb') as handle:
pickle.dump(raw_X, handle)
同样:
raw_X = (self.token_ques(text) for text in training_data)
with open('/root/Desktop/classifier_result.pkl', 'wb') as handle:
pickle.dump(list(raw_X), handle)
关于python - 类型错误 : can't pickle generator objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28963354/
假设我有一个 A 类和一个派生自 A 的 B 类。我想 pickle/unpickle B 类的一个实例。A 和 B 都定义了 __getstate__/__setstate__ 方法(假设 A
似乎有两种方法可以将指标推向 Graphite /碳, 线路接收器 pickle 接收器 根据文档 http://graphite.readthedocs.org/en/1.0/feeding-car
Perforce命令行有一个特殊的开关-G,它使用python的“pickle”序列化格式可以使输出成为机器可读的。一般来说,实际上是这样吗? 例如,考虑p4 -G diff -duw3 的输出。
如何从 BytesIO 对象写入和读回 pickled 数据? 我尝试过: import io import cPickle as pickle s1 = "foo" bytes_io = io.By
我有两个文件: x.py class BF(object) def __init__(): . . def add(self,z): . . y.py from y
在 post昨天发帖,无意中发现改了__qualname__函数对 pickle 有意想不到的影响.通过运行更多测试,我发现在对函数进行 pickle 时,pickle不像我想的那样工作,改变 __q
为什么 pickle 重用现有的 Python 类“C”而不是从 pickle 字节重建类?有没有一种方法可以在没有副作用的情况下 pickle 和解 pickle ? 这是我的回复 session
我使用 mpi4py 将一些计算拆分到多个过程中。基本上我只是计算一些凸包的体积,这是我使用 tvtk 和 mayavi 创建的。 只有第一个过程导入这些库: ... if rank==0: f
我正在用 pygame 制作一个绘图程序,我想在其中为用户提供一个选项来保存程序的确切状态,然后在稍后重新加载它。在这一点上,我保存了我的全局字典的副本,然后遍历, pickle 每个对象。 pyga
所以,我有一个对象,里面有很多不可 pickle 的东西(pygame 事件、orderedDicts、时钟等),我需要将它保存到磁盘。 事情是,如果我可以让这个东西存储一个有进度的字符串(我只需要一
import pickle variety = ["sweet", "box", "cat"] shape = ["back","spear", "log"] pickleFile = open("
我有一个关于 gensim 的问题。我想知道在保存或加载模型(或多个模型)时是否建议或需要使用 pickle,因为我在 GitHub 上找到了可以使用的脚本。 mymodel = Doc2Vec(do
我正在使用 python3.6/。我使用 protocol=pickle.HIGHEST_PROTOCOL pickle 了我的文件 当我按如下方式加载时: with open('data.sav',
给定一个像这样的任意Pythonic对象: class ExampleObj(object): def __init__(self): self.a = 'a'
简介 我有一本具有以下格式的字典: dict_list = {'S0':[[list of int],[list of int]], 'S1':[[list of int],[list of int]
我想知道这个错误可能意味着什么: PicklingError: Can't pickle : attribute lookup __builtin__.function failed 我理解这与使用多
我对 python 变量持久性有点困惑,在我的代码中,我使用以下代码使模型参数在某些迭代期间持久化 with open('W_Hs_Hu_iter'+str(inx)+'.pickle', 'wb'
当对象通过其属性之一引用自身时,从带有插槽的类中挑选对象的正确方法是什么?这是一个简单的示例,使用我当前的实现,我不确定它是否 100% 正确: import weakref import pickl
我有数千个长 (8640) 整数列表元组。例如: type(l1) tuple len(l1) 2 l1[0][:10] [0, 31, 23, 0, 0, 0, 0, 0, 0, 0] l1[1][
我有一个对象 gui_project,它有一个属性 .namespace,这是一个命名空间字典。 (即从字符串到对象的字典。) (这在类似 IDE 的程序中使用,让用户在 Python shell 中
我是一名优秀的程序员,十分优秀!