【发布时间】:2017-01-26 10:41:53
【问题描述】:
所以我正在尝试编写一个简单的 Twitch.tv IRC 机器人。机器人读取通道中的传入消息,如果消息匹配某些模式,机器人将执行某些任务。我遇到的问题是,如果用户输入某些 unicode 字符(即,如果用户输入“¯_(ツ)_/¯”,程序将抛出错误并崩溃:
UnicodeEncodeError 未被用户代码处理
'charmap' 编解码器无法对位置 13 中的字符 '\xaf' 进行编码:字符映射到
现在,我希望我的程序能够处理这些输入,但我不知道要更改或添加到我的代码中以启用它。这是我的代码:
http://pastebin.com/EBTaqpbZ(我不知道如何使用 Stackoverflow 代码粘贴)
我收到错误的代码的主要部分是:
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)
错误似乎总是发生在代码行:print (username + ": " + message)
有人知道我应该如何处理这些 unicode 字符吗?
【问题讨论】:
标签: python unicode bots irc twitch