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

查看:229
本文介绍了python3.x - windows下python3,print列表字符编码问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

解决方案

import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

Good luck!

这篇关于python3.x - windows下python3,print列表字符编码问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆