【发布时间】:2020-03-20 03:28:20
【问题描述】:
我看过所有这些以及更多,这不是重复的! (好吧,但我需要一个答案)。
How to overwrite multiline print in Python?
How can I remove last printed line in python?
Output to the same line overwriting previous output?
Overwrite the previous print value in python?
但其中甚至没有一个答案实际上会覆盖 Python 3.8 中的打印字符。
那么让我问一下如何在 Python 中覆盖最后打印的字符?
例如在这段代码中:
import time
print("Line one",end="\r")
time.sleep(2)
print ("Line two")
在这段代码中,我希望将“第一行”替换为“第二行”。
或许,
import time
print("1")
time.sleep(1)
print("2")
time.sleep(1)
print("3")
这样每个数字都替换了最后一个。
【问题讨论】:
-
为什么需要
time模块? -
只是在创建和替换行之间添加一个时间延迟。
-
你的输出去哪儿了?例如,在 IDLE shell 中,您可以做的绝对 nothing 会导致覆盖先前的输出 - 它只是将字符附加到 Text 小部件。
-
您的代码对我有用。请注意,第二行至少需要与第一行一样长,否则您仍然会看到第一行的结尾。
-
确保您在终端模拟器中运行代码,而不是像 IDLE 这样的调试器。
标签: python