TypeError: coercing to Unicode: need string or buffer, int found ( Str method doesn't work )
<p>i am trying to conclude the unfinished module, but i'm having major problem which i can't find fix for, Also note, that i am doing this in Python2. Full code is <a href="https://github.com/GTB3NW/SteamTradingServices/blob/master/Exchange/exchange.py" rel="nofollow">here</a>.</p>

<p>When the code was executed, the first problem i got was in line 69:</p>

<p>Full Log:</p>

<pre><code>&gt;&gt;&gt; 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 "&lt;stdin&gt;", line 1, in &lt;module&gt;
  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
</code></pre>

<p>So i tried converting it to unicode string:</p>

<pre><code>    log.debug("Connecting to redis on " + str(self.redishost) + ":" + str(self.redisport) + " db: " + str(self.redisdb))
</code></pre>

<p>But it gave me the same error on the same line:</p>

<pre><code>  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
</code></pre>

<p>I also tried another way of converting string, but same error: </p>

<pre><code>  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
</code></pre>

<hr>

<p>What may the problem be? Even when i tried to covert it to the string it gave me the same error unfortunately... Do i need to import something from <code>__futre__</code> that would help the problem?</p>

<p>The <em>coercing</em> error happens when trying so do an operation with a unicode and something which cannot be automatically converted to unicode.</p>

<p>For example:</p>

<pre><code>&gt;&gt;&gt; u'something' + 11
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: coercing to Unicode: need string or buffer, int found
</code></pre>

<p>On the other hand, with <code>unicode</code> and <code>str</code> it will work - <code>str</code> will coerce to <code>unicode</code> before applying the operation:</p>

<pre><code>&gt;&gt;&gt; u'something' + 'else'
u'somethingelse'
</code></pre>

<p>In the example, there are strings and variables (of unknown type), so it has to be that at least one of the variables was <code>unicode</code> and one an <code>int</code>, hence the <em>"coercing to Unicode"</em> error.</p>

<h2>General Solution</h2>

<p>Any of these will work:</p>

<pre><code>&gt;&gt;&gt; u'aaaa ' + str(9) + ':' + str(15)
u'aaaa 9:15'
&gt;&gt;&gt; u'aaaa %s: %s' % (9, 15)
u'aaaa 9: 15'
&gt;&gt;&gt; u'aaaa {}: {}'.format(9, 15)
u'aaaa 9: 15'
</code></pre>

<h2>Logging Solution</h2>

<p>The proper way to format logs would be:</p>

<pre><code>log.debug("Connecting to redis on %s: %s db: %s",
          self.redishost,
          self.redisport,
          self.redisdb)
</code></pre>

<p>I.e. you do not format the string - you let the logger do that.</p>

<p>That way, you avoid formatting the strings if the log level is disabled.</p>

<p>It also looks much better.</p>

python python-2.7 unicode