UnicodeEncodeError with Twitch.tv IRC bot
<p>So I'm trying to program a simple Twitch.tv IRC bot. The bot reads incoming messages in the channel, and if the messages match certain patterns, the bot performs certain tasks. The problem that I'm getting is that if a user inputs certain unicode characters (i.e. if the user enters "Â¯_(ã)_/Â¯", the program will throw the error and crash:</p>

<p>UnicodeEncodeError was unhandled by user code</p>

<p>'charmap' codec can't encode character '\xaf' in position 13: character maps to &lt; undefined ></p>

<p>Now, I want my program to be able to handle these inputs, but I have no idea what to change or add to my code to enable this. This is my code:</p>

<p><a href="http://pastebin.com/EBTaqpbZ" rel="nofollow">http://pastebin.com/EBTaqpbZ</a> (I couldn't figure out how to use Stackoverflow code paste)</p>

<p>The main part of the code that I'm receiving the error on is:</p>

<pre><code>while True:                                                     #Main Loop
    response = s.recv(1024).decode("utf-8")
    if response == "PING :tmi.twitch.tv\r\n":                   #If Ping, return Pong
        s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
        print("Pong Successful")
    else:                                                       #Else, Decode User Message
        username = re.search(r"\w+", response).group(0)         #Gets User
        message = CHAT_MSG.sub("", response)                    #Gets Message
        print (username + ": " + message)                       #Prints User Message
        if message.find("!hello") != -1:                        #Simple Test command to see if Reading Chat Input
            chat ("Hello! I'm speaking!\r\n")
    time.sleep(1 / cfg.RATE)
</code></pre>

<p>The error always seems to happen on the line of code: <code>print (username + ": " + message)</code> </p>

<p>does anyone know how I should go about handling these unicode characters?</p>

<p>(Would comment with a link to an answer but I do not have enough reputation yet.)</p>

<p>So, I assume you are using windows? What happens is that the encoding your console uses cannot print the unicode characters, and that causes the crash. </p>

<p>So the problem is not so much in the code itself, just the tools used. For example, the code runs fine when ran from a linux console. One way to overcome this problem seems to be using <a href="https://github.com/Drekin/win-unicode-console" rel="nofollow">win-unicode-console</a> to enable unicode input and output from windows console. See <a href="http://stackoverflow.com/a/32176732/6845813">this answer</a> for a broader description of the problem and solution.</p>

<p>You can also just go around the problem if you just need the print for debugging purposes:</p>

<pre><code>msg = username + ": " + message
print (msg.encode("utf-8")) 
</code></pre>

<p>However, that is not a real solution, and the output will be something like</p>

<blockquote>
  <p>b'\xc2\xaf_(\xe3\x83\x84)_/\xc2\xaf\r\n'</p>
</blockquote>

<p>for your example string, so not very convenient. I recommend reading the answer I linked.</p>

python unicode bots irc twitch