TypeError: should be a string, not a unicode

I have this code:

... msgdict = {'datafile': datafile, 'mapper': mapper, 'reducer':reducer} msg = cPickle.dumps(msgdict) print msg 

Print message msg I get this:

 (dp1 S'mapper' p2 (S's3n://myFolder/mapper.py' p3 tp4 sS'datafile' p5 (S's3n://myFolder/test.txt' p6 tp7 sS'reducer' p8 (S's3n://myFolder/reducer.py' p9 tp10 s. 

Then Im trying to get my content:

 for i in range(count): m = q[0].read() # this print returns a object Message print m # m.get_body()) returns the same of print msg above msg = cPickle.loads(m.get_body()) 

But I have this message:

 msg = cPickle.loads(m.get_body()) TypeError: must be string, not unicode 

Does anyone know how to solve this error?

+5
source share
1 answer

Try replacing this line as follows:

 msg = cPickle.loads(str(m.get_body())) 

m.get_body() str() in m.get_body() , it ensures that if the string is unicode, it converts it to a string.

+7
source

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


All Articles