TypeError: must be string, not unicode
TypeError: must be string, not unicode
我有这个代码:
...
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
有人知道如何解决这个错误吗?
尝试用以下内容替换该行:
msg = cPickle.loads(str(m.get_body()))
通过将 str()
转换为 m.get_body()
,它确保如果字符串是 unicode,它会将其转换为字符串。
我有这个代码:
...
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
有人知道如何解决这个错误吗?
尝试用以下内容替换该行:
msg = cPickle.loads(str(m.get_body()))
通过将 str()
转换为 m.get_body()
,它确保如果字符串是 unicode,它会将其转换为字符串。