2011-07-18 2 views
1

私はTwistedを使用するpythonで書かれたIRC botを持っています。方法:non-ascii文字列を受け入れるための非公開文字列

self.msg(channel, str.encode('utf-8')で問題なくASCII以外の文字列を印刷できます。

しかし、私は非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によって受信されたmsgのUTF-8ですか?

答えて

3

"エンコード"ではなく "デコード"したいと思います。 privmsgへの引数は、バイト文字列(str、Python 2.x)です。したがって、それをテキストにしたい場合は、 -codeこれらのバイトが必要です。

エンコーディングはサーバーから受信したものなので、エンコーディングをUTF-8に強制することはできません。 IRCの文字セットサポートが完全に不足しているので、これが最善の方法です。

関連する問題