删除 Python 中的最后一个输入行 [英] Delete the last input row in Python

查看:19
本文介绍了删除 Python 中的最后一个输入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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 following http://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 中的最后一个输入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆