我在Ubuntu 11.10(Linux)上使用Python 3.2。我的新代码看起来像这样:
text = input("TEXT=")
提示后是否可以获得一些预定义的字符串,所以如果需要我可以调整它?它应该是这样的:
python3 file
TEXT=thepredefinedtextishere
现在我按 Backspace 3次
TEXT=thepredefinedtextish
现在我按 Enter ,变量text
应为thepredefinedtextish
答案 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