gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:59 25 4
gpt4 key购买 nike

在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)

在终端中:

~$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

有没有人知道 Python 的 unicode 字符串在终端中工作但在 emacs 中不工作的原因

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

最佳答案

我相信这是相关的: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")

你的 .emacs 为我解决了这个问题。

关于python - 为什么 UnicodeEncodeError 只在 emacs 的 python shell 中引发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14172576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com