解决方案有两种:
- 在命令行前指定编码
$ PYTHONIOENCODING=utf-8 python test.py
hello world
你好,世界
- 在代码中指定编码
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
print('hello world')
print('你好,世界')
3.终极解决方案是:指定系统的编码,将以下内容加入到系统配置文件中
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8