为什么 UnicodeEncodeError 仅在 emacs 的 python shell 中引发?

Han*_*Sun 3 python unicode emacs ascii

在 emacs 中(使用run-python命令调用python3):

>>> sys.version
sys.version
'3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> sys.getdefaultencoding()
sys.getdefaultencoding()
'utf-8'
>>> data
data
'sp\xe4m'
>>> print(data)
print(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 2: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

在终端:

~$python3
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> data='sp\xe4m'
>>> sys.getdefaultencoding()
'utf-8'
>>> data
'späm'
>>> print(data)
späm
Run Code Online (Sandbox Code Playgroud)

有没有人对 Python 的 unicode 字符串在终端中有效但在 emacs 中无效的原因有任何想法

(emacs 的版本信息 : GNU Emacs 24.2.1 (x86_64-apple-darwin, NS apple-appkit-1038.36) of 2012-08-27 on bob.porkrind.org)

Dro*_*ror 5

我相信这是相关的:https : //emacs.stackexchange.com/a/9876/861

添加

(setenv "LANG" "en_US.UTF-8")
(setenv "LC_ALL" "en_US.UTF-8")
(setenv "LC_CTYPE" "en_US.UTF-8")
Run Code Online (Sandbox Code Playgroud)

.emacs为我解决了问题。