Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)

现象

打印任何一种包含有中文的对象,字典、列表、DataFrame、或字符串。比如:

print('中文')

控制台报错:

Traceback (most recent call last):
  File "printcn.py", line 1, in <module>
    print('\u4e2d\u6587')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

换另外一台机器可以正常显示 中文 。或者在PyCharm里执行也可以正常显示。只有在命令行控制台会报错。

我的环境是MacOS 10.13.3 中文,Anaconda3 5.0.1

Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct  6 2017, 12:04:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

查找原因

如果是python 2.X的话需要在文件中加上 # -*- coding: utf-8 -*- 、以及 reload(sys) sys.setdefaultencoding("utf8") 。但是Python3应当默认就使用utf8编码,而且即使设置了这些也仍然不能正常打印。

有些人说用encode('utf-8')函数解决,但如果直接打印字典或DataFrame,总不能每个元素都encode一般吧。

最终查看了一下系统环境编码

>>> import sys
>>> sys.stdout.encoding
'US-ASCII'

而另一台能正常打印的机器是 en_US.UTF-8 

解决办法

(1)设置环境变量LANG

在linux或Mac上设置环境变量的方式一样,编辑~/.bash_profile文件('~'指的是用户登录后的默认目录),添加一行:

export LANG="en_US.UTF-8"

保存退出后重新打开命令行控制台

(2)使用PYTHONIOENCODING

在运行python命令前添加参数 PYTHONIOENCODING=utf-8 python printcn.py 

该参数的解释可查看官方文档:https://docs.python.org/3.6/using/cmdline.html#envvar-PYTHONIOENCODING

(3)重新定义标准输出

在代码中添加 sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) ,使代码变为:

import sys
import codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
print('中文')

 

posted @ 2018-03-22 11:22  qhlblog  阅读(12622)  评论(0编辑  收藏  举报