如何:扭曲 privmsg 以接受非 ascii 字符串
我有一个用 python 编写的 IRC 机器人,使用 Twisted。
它可以打印非 ASCII 字符串,而不会出现 self.msg(channel, str.encode('utf-8')
的问题。
但是,当使用 接收非 ASCII 字符串时,我会遇到异常privmsg:
def privmsg(self, user, channel, msg):
msg = msg.encode('utf-8')
user = user.split('!', 1)[0]
[... code goes here...]
我收到以下异常:
File "/usr/lib64/python2.4/site-packages/twisted/words/protocols/irc.py", line 1498, in handleCommand
method(prefix, params)
File "/usr/lib64/python2.4/site-packages/twisted/words/protocols/irc.py", line 1043, in irc_PRIVMSG
self.privmsg(user, channel, message)
File "./IlyBot.py", line 58, in privmsg
msg = msg.encode('utf-8')
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 4: ordinal not in range(128)
有谁知道如何在 privmsg 收到的消息上强制编码为 UTF-8?
I have an IRC bot written in python that uses Twisted.
It can print non-ascii strings without a problem with self.msg(channel, str.encode('utf-8')
.
However, I get exceptions when a non-ascii string is being received with privmsg:
def privmsg(self, user, channel, msg):
msg = msg.encode('utf-8')
user = user.split('!', 1)[0]
[... code goes here...]
I get the following exception:
File "/usr/lib64/python2.4/site-packages/twisted/words/protocols/irc.py", line 1498, in handleCommand
method(prefix, params)
File "/usr/lib64/python2.4/site-packages/twisted/words/protocols/irc.py", line 1043, in irc_PRIVMSG
self.privmsg(user, channel, message)
File "./IlyBot.py", line 58, in privmsg
msg = msg.encode('utf-8')
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 4: ordinal not in range(128)
Does anyone know how to force the encoding to be UTF-8 on the msg received by privmsg?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想要“解码”,而不是“编码”。
privmsg
的参数是一个字节字符串(str
,在 python 2.x 中),因此如果您希望它是文本,则必须de-对这些字节进行编码。您不能强制编码为 UTF-8,因为编码是您从服务器接收到的编码。由于 IRC 完全缺乏字符集支持,这是您能做的最好的事情。
I think you want "decode", not "encode". The argument to
privmsg
is a byte string (str
, in python 2.x), so if you want it to be text you have to de-code those bytes.You can't force the encoding to be UTF-8, because the encoding is whatever you happened to receive from the server. Thanks to IRC's complete lack of character set support, that's the best you can do.