在python3中使用readline自动完成
问题描述:
我试图用这个漂亮的技巧here来处理一个csv文件。虽然我似乎无法使用python3自动完成工作。我不知道从readline开始。文件有点密集。我的猜测是,我错过了Python 2中没有raw_input()
的东西。在python3中使用readline自动完成
我粘贴了下面的我的尝试。当我在shell中,并且我点击标签时,我只会看到大标签并且没有自动完成操作。我的意图是,下面的输入语句自动完成字符串['10/10/2013', '10/13/2013', '10/14/2013', '10/15/2013']
。
我错过了什么?
import readline
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
if state == 0: # on first trigger, build possible matches
if text: # cache matches (entries that start with entered text)
self.matches = [s for s in self.options
if s and s.startswith(text)]
else: # no text entered, all matches possible
self.matches = self.options[:]
# return match indexed by state
try:
return self.matches[state]
except IndexError:
return None
dates = [
'10/10/2013 13:03:51',
'10/10/2013 13:54:32',
'10/10/2013 18:48:48',
'10/10/2013 19:13:00',
'10/13/2013 12:58:17',
'10/13/2013 13:38:15',
'10/13/2013 16:48:58',
'10/13/2013 17:23:59',
'10/13/2013 20:09:56',
'10/13/2013 21:54:14',
'10/13/2013 21:57:43',
'10/13/2013 22:47:40',
'10/14/2013 13:32:53',
'10/14/2013 21:14:51',
'10/15/2013 10:18:23'
]
dates = [x.split(' ')[0] for x in dates]
completer = MyCompleter(list(set(dates)))
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
date = input('Enter a date in m/d/yy format\n\t')
更新:下面的答案很好,但在OS X上仍然存在问题。我甚至不知道从哪里开始排除故障。我在Ubuntu上获得了这个自动完成功能,但是它在我的OS X系统上不会绑定到tab
。
答
修正版本:
from __future__ import print_function
import sys
import readline
from os import environ
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
if state == 0: # on first trigger, build possible matches
if not text:
self.matches = self.options[:]
else:
self.matches = [s for s in self.options
if s and s.startswith(text)]
# return match indexed by state
try:
return self.matches[state]
except IndexError:
return None
def display_matches(self, substitution, matches, longest_match_length):
line_buffer = readline.get_line_buffer()
columns = environ.get("COLUMNS", 80)
print()
tpl = "{:<" + str(int(max(map(len, matches)) * 1.2)) + "}"
buffer = ""
for match in matches:
match = tpl.format(match[len(substitution):])
if len(buffer + match) > columns:
print(buffer)
buffer = ""
buffer += match
if buffer:
print(buffer)
print("> ", end="")
print(line_buffer, end="")
sys.stdout.flush()
dates = [
'10/10/2013 13:03:51',
'10/10/2013 13:54:32',
'10/10/2013 18:48:48',
'10/10/2013 19:13:00',
'10/13/2013 12:58:17',
'10/13/2013 13:38:15',
'10/13/2013 16:48:58',
'10/13/2013 17:23:59',
'10/13/2013 20:09:56',
'10/13/2013 21:54:14',
'10/13/2013 21:57:43',
'10/13/2013 22:47:40',
'10/14/2013 13:32:53',
'10/14/2013 21:14:51',
'10/15/2013 10:18:23'
]
dates = [x.split(' ')[0] for x in dates]
completer = MyCompleter(list(set(dates)))
readline.set_completer_delims(' \t\n;')
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
readline.set_completion_display_matches_hook(completer.display_matches)
print('Enter a date in m/d/yy format\n\t')
date = input("> ")
说明(S):
- 添加自定义
display_matches()
(可能不适合你有用) - 新增
readline.set_completer_delims()
调用,因为我们希望将/
作为单词的一部分。
在Mac OS X
+0
好吧,我的系统肯定有问题。这在Ubuntu上适用于我,但我只是得到了OS X的实际选项卡。感谢您的清理。 – mrKelley
示例代码测试上的Python 3.3对我的作品有**的Python 3.3 ** albiet不是很好。你的''Completer()''我认为:)有一些缺陷。 –
Yikes,这听起来像你比我更进一步。我在OS X上,Python 3.3 ... – mrKelley
同样在这里! :)我试图解决你显示的匹配选项。但我可能不得不把这个留给其他人。我已经成功完成了。请参阅:https://bitbucket.org/prologic/mio-lang/src/tip/mio/state.py –