开发者

Python: Get user input by letting user edit default value

开发者 https://www.devze.com 2023-03-06 18:48 出处:网络
I\'ve got a script that pulls text from a Web server. 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

I've got a script that pulls text from a Web server. 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.

Is there a way to do this?

Thanks!


Yes, there is a way. Use 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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消