python3.x - windows下python3,print列表字符编码问题.

 mobiledu2502878137 发布于 2022-10-27 21:40

有一个列表.里面有不确定的字符,直接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
1 个回答
  • import sys, codecs
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

    Good luck!

    2022-11-12 01:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有