2013-12-17 90 views
1

我試圖用這個漂亮的技巧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

+0

示例代碼測試上的Python 3.3對我的作品有**的Python 3.3 ** albiet不是很好。你的''Completer()''我認爲:)有一些缺陷。 –

+0

Yikes,這聽起來像你比我更進一步。我在OS X上,Python 3.3 ... – mrKelley

+0

同樣在這裏! :)我試圖解決你顯示的匹配選項。但我可能不得不把這個留給其他人。我已經成功完成了。請參閱:https://bitbucket.org/prologic/mio-lang/src/tip/mio/state.py –

回答

2

修正版本:

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