我想结束未完成的模块,但是我遇到了一个我无法找到修复的主要问题,另外请注意,我在Python2中这样做。完整代码为here。
当代码执行时,我得到的第一个问题是在第69行:
完整日志:
>>> main()
INFO:Exchange.exchange:Exchange Service is starting
INFO:pika.adapters.base_connection:Connecting to 127.0.0.1:5672
INFO:pika.adapters.blocking_connection:Created channel=1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/click-6.6-py2.7.egg/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 38, in main
Exchange(rmqhost, redishost, redisport, redisdb, status, publishresult).consume()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 69, in __init__
log.debug("Connecting to redis on " + self.redishost + ":" + self.redisport + " db: " + self.redisdb)
TypeError: coercing to Unicode: need string or buffer, int found
所以我尝试将其转换为unicode字符串:
log.debug("Connecting to redis on " + str(self.redishost) + ":" + str(self.redisport) + " db: " + str(self.redisdb))
但它在同一行上给了我同样的错误:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 69, in __init__
log.debug("Connecting to redis on " + str(self.redishost) + ":" + str(self.redisport) + " db: " + str(self.redisdb))
TypeError: coercing to Unicode: need string or buffer, int found
我还尝试了另一种转换字符串的方法,但同样的错误:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Exchange/exchange.py", line 69, in __init__
log.debug("Connecting to redis on %s: %s db: %s",
TypeError: coercing to Unicode: need string or buffer, int found
问题可能是什么?即使我试图将它转换为字符串,它也不幸地给了我同样的错误......我是否需要从__futre__
导入一些有助于解决此问题的内容?
答案 0 :(得分:2)
当尝试使用unicode进行操作时会发生强制错误,并且无法自动转换为unicode。
例如:
>>> u'something' + 11
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, int found
另一方面,使用unicode
和str
它会起作用 - str
会在应用操作之前强制unicode
:
>>> u'something' + 'else'
u'somethingelse'
在示例中,有字符串和变量(未知类型),因此必须至少有一个变量是unicode
而另一个是int
,因此 “强制转换为Unicode”错误。
其中任何一个都可行:
>>> u'aaaa ' + str(9) + ':' + str(15)
u'aaaa 9:15'
>>> u'aaaa %s: %s' % (9, 15)
u'aaaa 9: 15'
>>> u'aaaa {}: {}'.format(9, 15)
u'aaaa 9: 15'
格式化日志的正确方法是:
log.debug("Connecting to redis on %s: %s db: %s",
self.redishost,
self.redisport,
self.redisdb)
即。你没有格式化字符串 - 你让记录器这样做。
这样,如果禁用日志级别,则可以避免格式化字符串。
它看起来也好多了。