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

有一个列表.里面有不确定的字符,直接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级讲师

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

Good luck!

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!