如何:扭曲 privmsg 以接受非 ascii 字符串

发布于 2024-11-24 23:32:07 字数 861 浏览 0 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-12-01 23:32:07

我认为你想要“解码”,而不是“编码”。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文