'ascii' codec can't encode characters in position

问题

在ubuntu中运行python3脚本输出到命令行出错

解决方案

将LANG 或 LC_ALL 设置为 en_US.utf8 或 zh_CN.utf8

  • 列出当前设置

    benull@37c7dedb7a13:~# locale
    LANG=zh_CN.UTF-8
    LANGUAGE=
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER="en_US.UTF-8"
    LC_NAME="en_US.UTF-8"
    LC_ADDRESS="en_US.UTF-8"
    LC_TELEPHONE="en_US.UTF-8"
    LC_MEASUREMENT="en_US.UTF-8"
    LC_IDENTIFICATION="en_US.UTF-8"
    LC_ALL=en_US.UTF-8
  • 列出已安装的语言环境

    benull@37c7dedb7a13:~# locale -a
    C
    C.UTF-8
    en_US.utf8
    POSIX
    zh_CN.utf8

如果不存在上述的 locale,先用 locale-gen 生成

apt-get install locales
locale-gen 'zh_CN.UTF-8'
update-locale LC_ALL="zh_CN.UTF-8"

export LC_ALL=zh_CN.UTF-8" 加到的~/.bashrc

参考

关于'ascii' codec can't encode characters in position 的问题

ubuntu locales 设置中文

Locale


NULL
168 声望16 粉丝

Tomorrow is another day!