用python3中的readline自动完成

2024-05-06 07:48:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用这个漂亮的技巧here来处理csv文件。不过,我似乎无法在python3中使用自动完成功能。我不知道从哪里开始读。文件有点密集。我的猜测是我遗漏了python2中没有raw_input()的内容。在

我把我的尝试贴在下面。当我在shell中点击tab时,我只得到大的标签页,而没有自动完成操作。我的目的是在字符串['10/10/2013', '10/13/2013', '10/14/2013', '10/15/2013']上自动完成下面的input语句。在

我错过了什么?在

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')

更新:下面是不错的答案,但在OSX上我仍然无法使用。我甚至不知道从哪里开始解决这个问题。我在Ubuntu上得到了自动完成功能,但不知何故它没有绑定到我的OS X系统上的tab。在


Tags: 文件textinselfinputreadlinereturnif
1条回答
网友
1楼 · 发布于 2024-05-06 07:48:04

更正版本:

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("> ")

注意事项:

  • 添加了一个自定义display_matches()可能对您没有用处)
  • 添加了readline.set_completer_delims()调用,因为我们要将/视为单词的一部分。在

在Mac OS X上的Python-3.3上测试

相关问题 更多 >