Python print函数编码限制问题:改变函数标准输出编码(Python3)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011304490/article/details/79946256
>>> print('\U0001f608')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    print('\U0001f608')
UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001f608' in position 0: Non-BMP character not supported in Tk
>>> 

或者

UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

解决方案:

import io
import sys

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8') # 改变标准输出的默认编码

标准终端输出:

<_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp936'>
阅读更多

没有更多推荐了,返回首页