当我从Mac OS的Terminal.app启动Python时,Python将编码识别为UTF-8:
如何让Emacs与Python通信,使得sys.stdout知道使用UTF-8?
编辑:由于我没有修改已接受答案的权限,在这里准确地描述了在Aquaemacs 1.6,Mac OS 10.5.6上为我工作的内容。
在python-mode-hook中,我添加了以下行:
$ 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