Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: must be string, not unicode

I have this code:

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

The print 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 Im having this errror:

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

Someone knows how to solve this error?

like image 715
UserX Avatar asked Mar 29 '15 18:03

UserX


1 Answers

Try to replace that line with the following:

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

By casting str() to m.get_body(), it makes sure that if the string is unicode, it converts it to a string.

like image 184
A.J. Uppal Avatar answered Nov 10 '22 15:11

A.J. Uppal