是否可以在Python 3的命令行界面中预填充输入()?

时间:2011-12-14 13:19:43

标签: python input python-3.x command-line-interface

我在Ubuntu 11.10(Linux)上使用Python 3.2。我的新代码看起来像这样:

text = input("TEXT=")

提示后是否可以获得一些预定义的字符串,所以如果需要我可以调整它?它应该是这样的:

python3 file
TEXT=thepredefinedtextishere

现在我按 Backspace 3次

TEXT=thepredefinedtextish

现在我按 Enter ,变量text应为thepredefinedtextish

1 个答案:

答案 0 :(得分:25)

如果您的Python解释器与GNU readline链接,input()将使用它。在这种情况下,以下内容应该有效:

def input_with_prefill(prompt, text):
    def hook():
        readline.insert_text(text)
        readline.redisplay()
    readline.set_pre_input_hook(hook)
    result = input(prompt)
    readline.set_pre_input_hook()
    return result