类型错误:必须是字符串,而不是unicode。

时间:2021-08-20 17:07:19

I have this code:

我有这段代码:

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

The print msg I get this:

打印msg我得到这个:

(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?

有人知道如何解决这个错误吗?

1 个解决方案

#1


7  

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.

通过将str()转换为m.get_body(),它确保如果字符串是unicode,它将它转换为字符串。

#1


7  

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.

通过将str()转换为m.get_body(),它确保如果字符串是unicode,它将它转换为字符串。