2013-11-14 114 views
1

我用下面的代码文本完成:完成者与国际字符

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 

def setCompleter(listOfItems): 
    readline.parse_and_bind('tab: complete') 
    readline.parse_and_bind('set editing-mode vi') 
    completer = MyCompleter(listOfItems) 
    readline.set_completer(completer.complete) 

的选项是从数据库中获取。当我需要完成时, 不提供包含国际字符和​​的选项。 我可以自定义代码以提供包含变音符号的选项吗?

+1

Python版本您使用的? –

回答

1

我怀疑你正在使用Python2;在Python3中这可能是“正常工作”。

您的数据库正在返回unicode对象,其中readline库在使用前转换为字符串。此转换默认使用ascii编解码器,该编解码器适用于u"Name",但对于u"Näme"不适用。

威力帮助替换此行:

completer = MyCompleter([item.encode('utf-8') for item in listOfItems]) 
+0

是的,我正在使用Python2 – xralf