我有这段代码:
...
msgdict = {'datafile': datafile, 'mapper': mapper, 'reducer':reducer}
msg = cPickle.dumps(msgdict)
print 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.
然后我试图获取我的内容:
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())
但我有这个错误:
msg = cPickle.loads(m.get_body())
TypeError: must be string, not unicode
有人知道如何解决这个错误吗?
答案 0 :(得分:7)
尝试使用以下内容替换该行:
msg = cPickle.loads(str(m.get_body()))
通过将str()
转换为m.get_body()
,它确保如果字符串是unicode,则将其转换为字符串。