I am trying to write some result on to pickle file as below:
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)
Error:
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle generator objects
Any help would be much appreciable.
This is a generator. As the error says, we cannot pickle generators. Use this instead.
And pickle
raw_X
then.Edit
This works for me
I'm using
str()
instead of your function anddumps()
instead ofdump()
.Don't use a generator expression when you want to pickle data. Use a list comprehension instead, or call
list()
on the generator to capture all generated elements for pickling.For example, the following works just fine:
as does: