简体   繁体   English

无法在python IDE中输入unicode(Mac OS X)

[英]Can't input unicode in python IDE (Mac OS X)

I'm trying to collect some unicode raw_input in the default python IDE, and as far as I'm aware, it should be as simple as: 我正在尝试在默认的python IDE中收集一些unicode raw_input,据我所知,它应该像下面这样简单:

>>> c = raw_input()
日本語
>>> print c
日本語

However, when I try to input the unicode characters, the computer beeps some protestations and I end up with an empty string. 但是,当我尝试输入unicode字符时,计算机会发出一些声音,最后会出现一个空字符串。 (To do this, I click on the IME switcher near the time and select the appropriate input method [which in this case is Japanese input). (为此,我在时间点击IME切换器并选择适当的输入方法[在本例中为日语输入]。 Outside of the python IDE, the input works fine, I can input the characters and the system recognizes them as having been input. 在python IDE之外,输入工作正常,我可以输入字符,系统将它们识别为已输入。 In the IDE, I'll type some hiragana, and the drop-down kanji selection window appears as usual, but when I select the appropriate representation and hit enter, those beeps come and I wind up with nothing. 在IDE中,我会输入一些平假名,并且下拉汉字选择窗口像往常一样出现,但是当我选择适当的表示并按下回车时,那些哔哔声会响起来,我什么都没有。 I figure there's a setting involved somewhere that I've missed. 我觉得我错过了一个涉及某处的环境。

versions are: 版本是:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

and

Python 2.5.4 (r254:67916, Jun 24 2010, 21:47:25) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

neither of which work. 两者都不起作用。 There's also this: 还有这个:

>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> sys.stdin.encoding
'UTF-8'
>>> sys.stdout.encoding
'UTF-8'
>>> sys.getfilesystemencoding()
'utf-8'

but from what I've read, the defaultencoding is a mysterious beast. 但从我所读到的,违约编码是一个神秘的野兽。 Changing it doesn't actually fix anything anyway. 改变它实际上并没有解决任何问题。 That is, 那是,

>>> import sys
>>> sys.setdefaultencoding('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setdefaultencoding'
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf-8')
>>> # !!!
... c = raw_input()
no dice!

doesn't work. 不起作用。 Just more beeping. 更多的哔哔声。 I can't cut-and-paste Japanese text from other applications, either. 我也不能从其他应用程序中剪切和粘贴日文文本。

The defaultencoding shouldn't affect here. 默认编码不应该影响到这里。 I had a similar problem and for me the solution was to check the Escape non-ASCII input option in Terminal > Preferences > Settings > Advanced. 我遇到了类似的问题,对我来说解决方法是在终端>首选项>设置>高级中检查Escape非ASCII输入选项。 Also make sure that the Character encoding is set to Unicode (UTF-8) in the same settings page. 还要确保在同一设置页面中将字符编码设置为Unicode(UTF-8)。

I've had the same problem. 我遇到了同样的问题。 In my case it turned out to be a libedit problem. 在我的情况下,它结果是一个libedit问题。 I fixed it by installing readline -- which I had to do from source (from here: http://pypi.python.org/pypi/readline ) since using pip or easy_install , for whatever reason, didn't actually replace readline. 我通过安装readline来修复它 - 我必须从源代码(来自这里: http//pypi.python.org/pypi/readline ),因为使用pipeasy_install ,无论出于何种原因,实际上并没有替换readline。

If you have ipython installed, it will tell you on startup if you're using libedit . 如果您安装了ipython ,它会在启动时告诉您是否使用了libedit And, if you have the same experience I did, you'll see the same problems in both the python interpreter in Terminal and in ipython. 而且,如果你有相同的经验,你会在终端和ipython中的python解释器中看到同样的问题。 Once I got readline truly installed, and ipython no longer informed me that it was using libedit, the problems with entering Unicode disappeared in both python and ipython. 一旦我真正安装了readline,并且ipython不再告诉我它正在使用libedit,输入Unicode的问题在python和ipython中都消失了。

(Note: I also have bpython installed -- and, since it doesn't seem to use readline or libedit, but rather its own line-editing routines, entering Unicode in bpython always worked.) (注意:我也安装了bpython - 并且,因为它似乎不使用readline或libedit,而是使用自己的行编辑例程,所以在bpython中输入Unicode 总是有效。)

Edit: I tried Python from the command line (Terminal), and it does not work, and I get the beeps you are talking about. 编辑:我从命令行(终端)尝试了Python,它不起作用 ,我得到你正在谈论的哔哔声。 It doesn't seem to be a Terminal limitation, as I can paste the characters at the $ prompt in bash just fine. 它似乎不是终端限制,因为我可以在bash中的$ prompt中粘贴字符就好了。 It does work in Idle, as I show below. 它在Idle中工作,如下所示。

Edit #2: Interestingly, this one-liner does work: 编辑#2:有趣的是,这个单线程确实有效:

 $ python -c "exec(\"c=raw_input()\nprint c\")"
 日本語  <-- pasted
 日本語

I'd put this in a comment, but it wouldn't format correctly. 我把它放在评论中,但它不能正确格式化。 Output from 2.6.5 on MacOSX: MacOSX上2.6.5的输出:

Python 2.6.5 (r265:79359, Mar 24 2010, 01:32:55) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 2.6.5      
>>> c=raw_input()
日本語
>>> print c
日本語
>>> c
u'\u65e5\u672c\u8a9e'
>>> 

Try this: 试试这个:

import codecs, sys
sys.stdin = codecs.getreader('UTF-8')(sys.stdin)
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
sys.stderr = codecs.getwriter('UTF-8')(sys.stderr)

print u'\u65e5\u672c\u8a9e'

This works for me for non-ASCII characters when using Putty with the terminal encoding set to UTF-8. 当使用Putty并将终端编码设置为UTF-8时,这适用于非ASCII字符。 I see boxes because I do not have fonts for CJK characters installed, but I think this should do it for you. 我看到盒子,因为我没有安装CJK字符的字体,但我认为这应该适合你。

The reason this works is that by default the Python interpreter uses the 'ascii' codec for stdin, stdout and stderr. 这样做的原因是,默认情况下,Python解释器对stdin,stdout和stderr使用'ascii'编解码器。 And because ASCII only defines byte values 0 to 127, only those byte values can be printed. 并且因为ASCII仅定义字节值0到127,所以只能打印那些字节值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM