当我从Mac的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?
编辑:由于我没有编辑被接受的答案的代表,下面就是在Aquaemacs1.6,MacOS10.5.6上对我起作用的原因。
在python模式钩子中,我添加了行。
(setenv "LANG" "en_GB.UTF-8")显然,Mac需要"UTF-8",而dfa则说Ubuntu需要"UTF8“。
此外,我必须通过执行C,然后输入"utf-8“两次来设置输入/输出编码。我也许应该找出如何永久地设置它。
感谢dfa和Jouni共同帮助我找到答案。
下面是我的最后一个python模式钩子:
(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发布于 2009-05-20 15:00:14
检查您的环境变量:
$ LANG="en_US.UTF8" python -c "import sys; print sys.stdout.encoding"
UTF-8
$ LANG="en_US" python -c "import sys; print sys.stdout.encoding"
ANSI_X3.4-1968在python钩子中,尝试:
(setenv "LANG" "en_US.UTF8")https://stackoverflow.com/questions/888406
复制相似问题