在控制台输出一个列表,结果报编码错误..
E:\weixin\itchat>python test.py Login successfully as 一二三 Start auto replying 1111 Traceback (most recent call last): File "test.py", line 27, in <module> itchat.run() File "E:\Program Files (x86)\Python35-32\lib\site-packages\itchat\__init__.py", line 120, in run configured_reply() File "E:\Program Files (x86)\Python35-32\lib\site-packages\itchat\__init__.py", line 96, in configured_reply if replyFn: send(replyFn(msg), msg.get('FromUserName')) File "test.py", line 8, in text_reply print(itchat.get_chatrooms()) UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f338' in position 2739: illegal multibyte sequence
然后用了decode还报错,不知道该怎么办了
Login successfully as 一二三 Start auto replying 1111 Traceback (most recent call last): File "test.py", line 28, in <module> itchat.run() File "E:\Program Files (x86)\Python35-32\lib\site-packages\itchat\__init__.py", line 120, in run configured_reply() File "E:\Program Files (x86)\Python35-32\lib\site-packages\itchat\__init__.py", line 96, in configured_reply if replyFn: send(replyFn(msg), msg.get('FromUserName')) File "test.py", line 9, in text_reply print(qun.decode('GBK')) AttributeError: 'list' object has no attribute 'decode'
源码如下
#coding=utf8 import itchat, time from itchat.content import * @itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) def text_reply(msg): print("1111") qun=itchat.get_chatrooms() print(qun.decode('GBK')) itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName']) @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) def download_files(msg): msg['Text'](msg['FileName']) return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName']) @itchat.msg_register(FRIENDS) def add_friend(msg): itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录 itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName']) @itchat.msg_register(TEXT, isGroupChat=True) def text_reply(msg): if msg['isAt']: itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName']) itchat.auto_login(True) itchat.run()
三叔2016-11-05 11:32:50
1.将unicode字符编码成GBK出现问题,因为本身Unicode类型的字符中,包含了一些无法转换为GBK编码的一些字符
如下图,
2.list列表对象是不支持直接编码的,但我测试发现没有这个问题出现
解决方法:
unicode字符编码时,添加ignore参数,忽略无法无法编码的字符 即str.encode('gbk', 'ignore')这种格式