简体   繁体   English

Python readline和UTF-8

[英]Python readline and UTF-8

I'm trying to use raw_input() with readline and utf-8 encoding (on OSX 10.6.8). 我正在尝试使用带有readline和utf-8编码的raw_input()(在OSX 10.6.8上)。

Imagine the following snippet: 想象一下以下片段:

import readline

while True: 
    try:
        inp = raw_input('> ')
    except EOFError:
        break

This works except when trying to type non standard ASCII letters, say 'å' or 'ä'. 除了尝试键入非标准ASCII字母(例如'å'或'ä')时,此方法有效。 When doing this no input is written, I guess readline is filtering this somehow. 当这样做没有写入输入时,我猜readline正在以某种方式过滤它。

I then changed the input encoding using 然后我使用改变了输入编码

sys.stdin = codecs.getwriter('utf-8')(sys.stdin)

Now typing 'å' and 'ä' works but it seems the readline functionality is no more. 现在键入'å'和'ä'可以工作,但似乎readline功能已不复存在。

Any ideas on how to make readline cope with unicode letters? 关于如何使readline处理unicode字母的任何想法?

(Reposting as an answer): OSX ships with an inferior equivalent to readline called libedit (for licensing reasons, it can't ship readline itself). (重新作为答案):OSX附带的等效于readline的libadeit(由于许可原因,它不能自己发送readline)。

You can do easy_install readline to get GNU readline in Python on a Mac. 您可以使用easy_install readline在Mac上获取Python中的GNU readline。

This also causes a problem for IPython, which now displays an ugly warning if it detects that libedit is in use. 这也会导致IPython出现问题,如果它检测到libedit正在使用中,它现在会显示一个丑陋的警告。

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

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