TypeError: cannot sort generator objects

I am trying to write some result to sort a file as shown 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 very noticeable.

+5
source share
2 answers

Do not use a generator expression when you want to sort data. Instead, use list comprehension or call list() on the generator to capture all the generated elements for etching.

For example, the following works just fine:

 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) 

as well as:

 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) 
+3
source
 raw_X = (self.token_ques(text) for text in training_data) 

This is a generator. As stated in the error, we cannot disassemble the generators. Use this instead.

 raw_X=[] for text in data: raw_X.append(self.token_ques(text)) raw_X=tuple(raw_X) 

And raw_X 's raw_X then.


Edit

It works for me

 import pickle raw_X=[] data=[1,2,3,4,5,6,2,0] for text in data: raw_X.append(str(text)) print pickle.dumps(raw_X) 

I use str() instead of your function and dumps() instead of dump() .

+3
source

Source: https://habr.com/ru/post/1215051/


All Articles