python3 输出中文报错UnicodeEncodeError: ‘ascii‘ codec can‘t encode解决方式

print("未获取到版本信息")

报错如下:

 print("\u672a\u83b7\u53d6\u5230\u7248\u672c\u4fe1\u606f")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

1. 首先查看编码

root@bb971a8c212:/app/# locale
LANG=LC_ALL=zh_CN.utf8
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C

输入:

echo 'export LC_ALL=C.UTF-8;unset LANGUAGE' >>  /etc/profile
echo 'export LANG="LC_ALL=zh_CN.utf8"' >>  /etc/profile
source /etc/profile

再查看下编码:

root@bb971a8c212:/app/# locale
LANG=LC_ALL=zh_CN.utf8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=C.UTF-8

 然后就可以输出中文啦 ~

 

上一篇:P4774 [NOI2018] 屠龙勇士 - 数论、中国剩余定理


下一篇:我用python写了一个模拟鼠标的软件