如何使用 `print` 函数清除控制台行
How to clear console line using `print` function
前提
我试图在不使用空格的情况下基本上清除控制台行,但不是整个控制台 window,这样我就不会从上次打印的内容中得到额外的字符。例如:
# This causes characters from the last thing printed:
print("I don't know you.", end="\r")
print("Hello Jim!", end="\r")
# Yields the following (without the quotations) -->
# "Hello Jim!ow you."
现在解决这个问题可以这样做:
import os
def new_print(message, end):
"""
Clears console, no matter the size of the previous line
without wrapping to a new line
"""
width_of_console = int(os.popen("stty size", "r").read().split()[1])
# = 17
print(f"{message :<{width_of_console}}", end=end)
new_print("I don't know you.", end="\r")
new_print("Hello Jim!", end="\r")
# Yields the following (without the quotations) -->
# "Hello Jim! "
问题
我如何
- 只是打印出
"Hello Jim!"
而不是"Hello Jim! "
(显然都没有引号)
- 清除该行
- 虽然没有清除整个控制台(这样我就有了除最后一行之外的其他内容的输出)
具体来说,当改变尺寸(从 17 到 30 的控制台宽度)时,控制台中会发生这样的事情,在我的例子中,这种情况经常发生:
Hello Jim! Hello Jim!
Hello Jim! Hello Jim
! Hello Jim! Hello
Jim! Hello Jim! H
ello Jim! Hello Jim!
我愿意接受一种全新的做事方式,比如使用 urwid 或类似的东西。
您可以使用 EL(擦除线)control sequence。在Python中,最简单的构造方法是:
"3[2K"
数字控制 EL 序列的行为:
- 0:向前清除直到行尾(默认)
- 1: 向后清除直到行首
- 2: 清除整行
EL序列不移动光标
此行为相当标准,但如果您想确定,可以使用 tput
.
查询 terminfo 数据库
tl;博士:
print("I don't know you.", end="\r")
print("3[2KHello Jim!", end="\r")
您可以执行如下操作。为了更好的可见性,我添加了睡眠时间。
import time
def myprint(msg):
print(msg, end='', flush=True)
time.sleep(1)
print('\r', end='', flush=True)
print(' ' * len(msg), end='', flush=True)
print('\r', end='', flush=True)
print('These are some other lines 1')
print('These are some other lines 2')
for i in range(10):
myprint('Hello Jim {}!'.format(i))
前提
我试图在不使用空格的情况下基本上清除控制台行,但不是整个控制台 window,这样我就不会从上次打印的内容中得到额外的字符。例如:
# This causes characters from the last thing printed:
print("I don't know you.", end="\r")
print("Hello Jim!", end="\r")
# Yields the following (without the quotations) -->
# "Hello Jim!ow you."
现在解决这个问题可以这样做:
import os
def new_print(message, end):
"""
Clears console, no matter the size of the previous line
without wrapping to a new line
"""
width_of_console = int(os.popen("stty size", "r").read().split()[1])
# = 17
print(f"{message :<{width_of_console}}", end=end)
new_print("I don't know you.", end="\r")
new_print("Hello Jim!", end="\r")
# Yields the following (without the quotations) -->
# "Hello Jim! "
问题
我如何
- 只是打印出
"Hello Jim!"
而不是"Hello Jim! "
(显然都没有引号) - 清除该行
- 虽然没有清除整个控制台(这样我就有了除最后一行之外的其他内容的输出)
具体来说,当改变尺寸(从 17 到 30 的控制台宽度)时,控制台中会发生这样的事情,在我的例子中,这种情况经常发生:
Hello Jim! Hello Jim!
Hello Jim! Hello Jim
! Hello Jim! Hello
Jim! Hello Jim! H
ello Jim! Hello Jim!
我愿意接受一种全新的做事方式,比如使用 urwid 或类似的东西。
您可以使用 EL(擦除线)control sequence。在Python中,最简单的构造方法是:
"3[2K"
数字控制 EL 序列的行为:
- 0:向前清除直到行尾(默认)
- 1: 向后清除直到行首
- 2: 清除整行
EL序列不移动光标
此行为相当标准,但如果您想确定,可以使用 tput
.
tl;博士:
print("I don't know you.", end="\r")
print("3[2KHello Jim!", end="\r")
您可以执行如下操作。为了更好的可见性,我添加了睡眠时间。
import time
def myprint(msg):
print(msg, end='', flush=True)
time.sleep(1)
print('\r', end='', flush=True)
print(' ' * len(msg), end='', flush=True)
print('\r', end='', flush=True)
print('These are some other lines 1')
print('These are some other lines 2')
for i in range(10):
myprint('Hello Jim {}!'.format(i))