【发布时间】:2010-10-27 16:01:18
【问题描述】:
当我从 Mac OS 的 Terminal.app 启动 Python 时,python 将编码识别为 UTF-8:
$ python3.0
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
这对 python2.5 也是一样的。
但在 Emacs 中,编码是 US-ASCII。
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'US-ASCII'
如何让 Emacs 与 Python 通信,以便 sys.stdout 知道使用 UTF-8?
编辑:由于我没有代表来编辑接受的答案,这正是在 Aquaemacs 1.6、Mac OS 10.5.6 上对我有用的方法。
在python-mode-hook中,我添加了这一行
(setenv "LANG" "en_GB.UTF-8")
显然,Mac OS 需要“UTF-8”,而 dfa 说 Ubuntu 需要“UTF8”。
此外,我必须通过执行 C-x RET p 然后键入“utf-8”两次来设置输入/输出编码。我可能应该找出如何永久设置它。
感谢 dfa 和 Jouni 共同帮助我找到答案。
这是我最后的 python-mode-hook:
(add-hook 'python-mode-hook
(lambda ()
(set (make-variable-buffer-local 'beginning-of-defun-function)
'py-beginning-of-def-or-class)
(define-key py-mode-map "\C-c\C-z" 'py-shell)
(setq outline-regexp "def\\|class ")
(setenv "LANG" "en_GB.UTF-8"))) ; <-- *this* line is new
【问题讨论】:
-
注意:在 ubuntu 上 LANG 必须设置为 en_GB.UTF8,没有 -
-
谢谢,我更正了我的总结。
-
在 ubuntu 上没有破折号?我总是使用 UTF-8 格式,它可以工作。
标签: python emacs encoding utf-8 terminal