python - 如何删除控制台中输入的问题?

标签 python python-3.x

我想用新的输出覆盖输入。我想做同样的事情( https://stackoverflow.com/a/8436827/14079038 ),但我需要覆盖输入。

我的代码返回:

while not addIp.__eq__(''):
    ignoredIp.append(addIp)
    print(addIp)
    addIp = input('Would you like to add ip ? (None = no): ')
Would you like to add ip ? (None = no): 1.1.1.1
1.1.1.1
Would you like to add ip ? (None = no): 1.1.1.2
1.1.1.2
Would you like to add ip ? (None = no):

但我想要这样的东西:

10.11.100.66
10.11.100.67
10.11.100.68
10.11.100.69
10.11.100.70
Would you like to add ip ? (None = no):

最佳答案

如果您在同一行中写入,则在 print() 中使用 end='\r' ,它将不会移动到下一行。

但它不适用于将 \n 发送到控制台/终端的 input(),并且您无法阻止它。也许您会使用像 curses 这样的模块它可以在某些控制台/终端中使用特殊代码将光标移动到任何位置,然后您可以移回上一行。


在 Linux 终端上,您可以使用代码 \033[nA 向上移动 n 行。

这样您就可以使用 \033[1A 移动到上一行并打印文本来代替问题。

末尾还需要一些空格,以删除较长问题中的文本。

addIp = '?'
while addIp:
    addIp = input('Would you like to add ip ? (None = no): ')
    print('\033[1A' + addIp + ' '*40)

参见other codes 。 Linux 使用类似的代码来为文本着色。


还有一些模块适用于 Windows、Linux,

它们用于在文本模式下绘制小部件(TUI - 文本用户界面)


并非所有控制台/终端都能遵守这些代码 - IDE/编辑器中的控制台通常不遵守这些代码。

关于python - 如何删除控制台中输入的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63353362/

相关文章:

python-3.x - 如何在 win32 错误消息中读取 EXCEPINFO 元组?

python - 当使用Kivy Clock设置 bool 标志时,如何触发回调?

python - 为什么 numpy 在查找矩阵中的非零元素方面更快?

python - Django 1.10 中未定义“views”

python - 应该如何在 "steady state"中使用 sqlite3?

python - 非质数只包含 2,3,5,7 优化

python - 匹配 Domain.CCTLD 的正则表达式

python - 检查变量是否可以解包

不允许 Python 类型提示 Dict 语法错误可变默认值。使用 'default factory'

python - 在 python 中以递归方式对文件进行 glob 的干净方法