关于Python3 打印中文乱码问题

解决方案有两种:

  1. 在命令行前指定编码
$ PYTHONIOENCODING=utf-8 python test.py
hello world
你好,世界
  1. 在代码中指定编码
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
print('hello world')
print('你好,世界')

  3.终极解决方案是:指定系统的编码,将以下内容加入到系统配置文件中:
sudo apt-get install locales
查看系统支持的字符集:locale -a

拉取中文编码:locale-gen zh_CN.UTF-8

再次查看:locale-a, 发现多了ZH开头的字符集

修改文件/etc/default/locale
LANG = zh_CN.UTF-8(这个根据实际查询出来的名称修改)

重启机器(不一定需要)
             export LANG=zh_CN.UTF-8

python zh pth problem:
str = str.encode('utf-8')
原文地址:https://www.cnblogs.com/llfctt/p/11833383.html