python3.x - windows下python3,print列表字符编码问题.
PHP中文网
PHP中文网 2017-04-18 09:52:07
0
1
729

有一个列表.里面有不确定的字符,直接print会报错,源码如下

#coding=utf8
import itchat, time
import sys
from itchat.content import *

@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
    print("1111")
    qun=itchat.get_chatrooms()
    #print(repr)
    print(qun)
    #itchat.dumps()
    #for i in range(len(qun)):
        #print (i,qun[i].decode(sys.stdin.encoding, 'replace'))
        #print (i,qun[i].decode(sys.stdin.encoding,  'ignore'))
        #print (i,qun[i].decode('gbk', 'ignore').encode('utf-8'))
        #print (i,qun[i])
    itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])

itchat.auto_login(True)
itchat.run()

错误信息如下

 File "test.py", line 11, in text_reply
    print(qun)
UnicodeEncodeError: 'gbk' codec can't encode character '
\U0001f338' in position 2610: illegal multibyte sequence

如果我遍历列表打印的话,同样报错...问了好多人,都没问出办法....这python 不会这么坑吧?请问大神这应该怎么解决?

File "test.py", line 17, in text_reply
    print (i,qun[i])
UnicodeEncodeError: 'gbk' codec can't encode character '
\U0001f338' in position 129: illegal multibyte sequence
PHP中文网
PHP中文网

认证0级讲师

reply all(1)
大家讲道理
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

Good luck!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template