可以显示用于编辑 Python 输入的默认值吗? [英] Show default value for editing on Python input possible?
问题描述
python 是否有可能接受这样的输入:
<前>文件夹名称:下载但不是用户键入下载",它已经作为初始值存在.如果用户想将其编辑为下载",他所要做的就是添加一个s"并按 Enter.
使用普通输入命令:
folder=input('文件夹名称:')
我只能得到一个空白提示:
<前>文件夹名称:是否有一种简单的方法可以做到这一点,但我缺少这种方法?
标准库函数 input()
和 raw_input()
没有这个功能.如果您使用的是 Linux,则可以使用 readline
模块定义使用预填充值和高级行编辑的输入函数:
导入readlinedef rlinput(prompt, prefill=''):readline.set_startup_hook(lambda: readline.insert_text(prefill))尝试:return input(prompt) # 或 Python 2 中的 raw_input最后:readline.set_startup_hook()
Is it possible for python to accept input like this:
Folder name: Download
But instead of the user typing "Download" it is already there as a initial value. If the user wants to edit it as "Downloads" all he has to do is add a 's' and press enter.
Using normal input command:
folder=input('Folder name: ')
all I can get is a blank prompt:
Folder name:
Is there a simple way to do this that I'm missing?
The standard library functions input()
and raw_input()
don't have this functionality. If you're using Linux you can use the readline
module to define an input function that uses a prefill value and advanced line editing:
import readline
def rlinput(prompt, prefill=''):
readline.set_startup_hook(lambda: readline.insert_text(prefill))
try:
return input(prompt) # or raw_input in Python 2
finally:
readline.set_startup_hook()
这篇关于可以显示用于编辑 Python 输入的默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!