Python print函数编码限制问题:改变函数标准输出编码(Python3)
>>> 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'>