Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in(示例

Posted langqi250

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in(示例相关的知识,希望对你有一定的参考价值。

【转】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(\'中文\')

 

以上是关于Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in(示例的主要内容,如果未能解决你的问题,请参考以下文章

python3下安装aiohttp遇到过的那些坑

python3编写脚本时遇到的坑......

遇到的python3 不兼容 python2的地方

python3.5安装locust遇到的问题总结

Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in(示例

Python3.6安装tesserocr遇到的问题