[英]Python: Get user input by letting user edit default value
I've got a script that pulls text from a Web server. 我有一个脚本,可从Web服务器提取文本。 I want to give the user (me) an opportunity to edit that text so they can select which part to keep. 我想给用户(我)一个编辑文本的机会,以便他们可以选择保留哪个部分。 Ideally, it would be something like this: 理想情况下,应该是这样的:
editedText= raw_input(defaultText)
So, defaultText
is printed, the user edits it and presses enter, and the text as they've edited is assigned to editedText
. 因此,将打印defaultText
,用户对其进行编辑,然后按Enter键,并将编辑后的文本分配给editedText
。
Is there a way to do this? 有没有办法做到这一点?
Thanks! 谢谢!
Yes, there is a way. 是的,有办法。 Use readline 使用readline
import readline
defaultText = 'I am the default value'
readline.set_startup_hook(lambda: readline.insert_text(defaultText))
res = raw_input('Edit this:')
print res
Note that this is not a terribly portable solution, and I've only tested it on Linux :) 注意,这不是一个非常便携的解决方案,我只在Linux上测试过它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.