问题描述
我有以下代码:
num = int(raw_input("input number: "))
print "" * 20
控制台输出看起来像
input number: 10
我想在用户按下 ENTER
后删除文本 input number: 10
.退格键 不行.
I'd like to delete the text input number: 10
after the user presses ENTER
. The backspace key can't do it.
推荐答案
这适用于大多数 unix 和 windows 终端......它使用非常简单的 ANSI 转义.
This will work in most unix and windows terminals ... it uses very simple ANSI escape.
num = int(raw_input("input number: "))
print " 33[A 33[A" # ansi escape arrow up then overwrite the line
请注意,在 Windows 上,您可能需要使用以下命令启用 ANSI 支持http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html
Please note that on Windows you may need to enable the ANSI support using the followinghttp://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html
" 33[A" 字符串被终端解释为将光标向上移动一行.
" 33[A" string is interpreted by terminal as move the cursor one line up.
这篇关于删除 Python 中的最后一个输入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!