我有一个跨越多行的字符串,导致终端滚动到最新打印的行。但我想留在第一行,这样我就能看到第一行,但看不到最新的。那可能吗? 例如:
for i in range(100):
print(f"hello {i}")
我想看到 hello 0
但输出应该保持相同的形状
最佳答案
如果您使用的是 GNU,则可以将 defscrollback 设置为 100。否则,这应该有帮助:
class More(object):
def __init__(self, num_lines):
self.num_lines = num_lines
def __ror__(self, other):
s = str(other).split("\n")
for i in range(0, len(s), self.num_lines):
print(*s[i: i + self.num_lines], sep="\n")
input("Press <Enter> for more")
more = More(num_lines=30)
"\n".join(map(str, range(100))) | more
关于python - 如何打印输出并保持在同一行(Python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986526/