Python 3 UnicodeEncodeError:'ascii'编解码器无法编码字符 [英] Python 3 UnicodeEncodeError: 'ascii' codec can't encode characters

查看:411
本文介绍了Python 3 UnicodeEncodeError:'ascii'编解码器无法编码字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Python,但是我已经遇到麻烦了.
我有一个只有一个命令的简单脚本:

I've just started to learn Python but I already ran into troubles.
I have a simple script with just one command:

#!/usr/bin/env python3
print("Příliš žluťoučký kůň úpěl ďábelské ódy.") # Text in Czech 

当我尝试运行此脚本时:

When I try to run this script:

python3 hello.py 

我收到此消息:

Traceback (most recent call last):
  File "hello.py", line 2, in <module>
    print("P\u0159\xedli\u0161 \u017elu\u0165ou\u010dk\xfd k\u016fn \xfap\u011bl \u010f\xe1belsk\xe9 \xf3dy.")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)

我正在使用Kubuntu 16.04和Python 3.5.2. 当我尝试此操作时:export PYTHONIOENCODING=utf-8它起作用,但只是暂时的.下次我打开bash时,遇到了同样的错误.

I am using Kubuntu 16.04 and Python 3.5.2. When I tried this: export PYTHONIOENCODING=utf-8 It worked but only temporarily. Next time I opened bash I got the same error.

根据 https://docs.python.org/3/howto/unicode.html#the-string-type Python源代码的默认编码为UTF-8.
因此,我已将源文件保存为UTF-8标识,Konsole设置为UTF-8,但仍然收到错误消息!
即使我添加

According to https://docs.python.org/3/howto/unicode.html#the-string-type the default encoding for Python source code is UTF-8.
So I have the source file saved id UTF-8, Konsole is set to UTF-8 but I still get the error!
Even if I add

# -*- coding: utf-8 -*-

从一开始它什么都不做.

to the beginning it does nothing.

另一个奇怪的事情:当我仅使用python而不是python3运行它时,它就可以工作.如何在Python 2.7.12中而不是在3.5.2中工作?

Another weird thing: when I run it using only python, not python3, it works. How is it possible to work in Python 2.7.12 and not in 3.5.2?

有任何永久解决此问题的想法吗?谢谢.

Any ideas for solving this permanently? Thank you.

推荐答案

感谢 Mark Tolen Alastair McCormack 提出问题所在.问题确实出在语言环境设置中.
当我运行locale时,输出为:

Thanks to Mark Tolen and Alastair McCormack for suggesting where the problem may be. The problem was really in the locale settings.
When I ran locale, the output was:

LANG=C
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC=cs_CZ.UTF-8
LC_TIME=cs_CZ.UTF-8
LC_COLLATE=cs_CZ.UTF-8
LC_MONETARY=cs_CZ.UTF-8
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT=cs_CZ.UTF-8
LC_IDENTIFICATION="C"
LC_ALL=

此"C"是使用ANSI charmap的默认设置.这就是问题所在.运行locale charmap给了我:ANSI_X3.4-1968,它不能显示非英文字符.
我使用Ubuntu文档站点修复了此问题.

This "C" is the default setting which uses the ANSI charmap. And that is where the problem was. Running locale charmap gave me: ANSI_X3.4-1968 which can not display non-English characters.
I fixed this using this Ubuntu documentation site.

我将这些行添加到/etc/default/locale:

LANGUAGE=cs_CZ.UTF-8
LC_ALL=cs_CZ.UTF-8

然后,您必须重新启动会话(注销并登录)以应用这些设置.

Then you have to restart your session (log out and in) to apply these settings.

运行locale现在会返回以下输出:

Running locale now returns this output:

LANG=C
LANGUAGE=cs
LC_CTYPE="cs_CZ.UTF-8"
LC_NUMERIC="cs_CZ.UTF-8"
LC_TIME="cs_CZ.UTF-8"
LC_COLLATE="cs_CZ.UTF-8"
LC_MONETARY="cs_CZ.UTF-8"
LC_MESSAGES="cs_CZ.UTF-8"
LC_PAPER="cs_CZ.UTF-8"
LC_NAME="cs_CZ.UTF-8"
LC_ADDRESS="cs_CZ.UTF-8"
LC_TELEPHONE="cs_CZ.UTF-8"
LC_MEASUREMENT="cs_CZ.UTF-8"
LC_IDENTIFICATION="cs_CZ.UTF-8"
LC_ALL=cs_CZ.UTF-8

并运行locale charmap会返回:

UTF-8

这篇关于Python 3 UnicodeEncodeError:'ascii'编解码器无法编码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆