夜猫的个人小站

       继续码起来

关于作者

微博北极熊硬糖
北京海淀区

python3在linux运行输出中文出错

标签   Python

py文件是#encoding=utf-8编码 然后执行时输出错误 print('\u2312') 这是默认输出编码为ascii。

可进入python执行环境下查看

import sys
sys.stdout

解决办法1:

If you get this error when you try to pipe/redirect output of your script

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

Just export PYTHONIOENCODING in console and then run your code.

修改.profile

添加一行:

PYTHONIOENCODING=utf-8

后执行source .profile

解决办法2:

在py文件开头加入:

import sys
import io
sys.stdout=io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

linux下各种错误

Nothing suggested above worked in my case (Ubuntu Server 12.04LTS). What finally helped was putting to the file /etc/environment:

LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8

参考链接1

参考链接2

最新评论

发表评论
回到顶部