比如如下代码:
#test.py
print(b'\xc2\xa0'.decode())
python test.py
没关系
python test.py > ques.txt
会报错
UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in position 0: illegal multibyte sequence
之后尝试设置环境变量
$env:PYTHONIOENCODING="UTF-8"
解决问题了,猜测是终端直接走stdio,编码会使用utf-8,但是重定向之后会使用GBK编码的原因。从而导致某些UTF-8的字符在GBK里面无法编码。
也可以直接设置:
import sys
sys.stdout.reconfigure(encoding='utf-8')
或者使用
python -X utf8 test.py
参考python3官方文档的说法可以解释这一现象:
https://docs.python.org/3/library/sys.html
使用一下代码进行测试,会发现重定向的和直接打印的有很大不同:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys, locale, os
print("IO encodings:")
print("stdin: {}".format(sys.stdin.encoding))
print("stdout: {}".format(sys.stdout.encoding))
print("stderr: {}".format(sys.stderr.encoding))
print("\ndefault encoding: {}".format(sys.getdefaultencoding()))
print("\ntty(-like) device?")
print("stdin: {}".format(sys.stdin.isatty()))
print("stdout: {}".format(sys.stdout.isatty()))
print("stderr: {}".format(sys.stderr.isatty()))
print("\nPYTHONIOENCODING: {}".format(os.environ.get("PYTHONIOENCODING")))