如何删除Python上最后一行打印的内容?

如何删除Python上最后一行打印的内容?,python,Python,我已经看到了删除单个字符的答案,但这会占用很多空间,我希望这能在相当多的行中工作。我希望给人留下移动光标的印象。这就是问题所在: sys.stdout.write ('virus_prevention.fix_virus.attempt_enter') sys.stdout.write('\033[2K\033[1G') time.sleep(2) print ('virus_prevention.fix.virus.|attempt_enter') time.sleep(2) sys.stdo

我已经看到了删除单个字符的答案,但这会占用很多空间,我希望这能在相当多的行中工作。我希望给人留下移动光标的印象。这就是问题所在:

sys.stdout.write ('virus_prevention.fix_virus.attempt_enter')
sys.stdout.write('\033[2K\033[1G')
time.sleep(2)
print ('virus_prevention.fix.virus.|attempt_enter')
time.sleep(2)
sys.stdout.write('\033[2K\033[1G')
print ('virus_prevention.fix.virus|.attempt_enter')
time.sleep(0.1)
sys.stdout.write('\033[2K\033[1G')
print ('virus_prevention.fix.viru|s.attempt.enter')
time.sleep(0.1)
sys.stdout.write('\033[2K\033[1G')
print ('virus_prevention.fix.vir|us.attempt.enter')
time.sleep(0.1)
sys.stdout.write('\033[2K\033[1G')
print ('virus_prevention.fix.vi|rus.attempt.enter')
time.sleep(0.1)
sys.stdout.write('\033[2K\033[1G')
print ('virus_prevention.fix.v|irus.attempt.enter')
time.sleep(0.1)
sys.stdout.write('\033[2K\033[1G')
print ('virus_prevention.fix.|virus.attempt.enter')
这是输出:

virus_prevention.fix_virus.attempt_enter
[2K[1Gvirus_prevention.fix.virus.|attempt_enter
[2K[1Gvirus_prevention.fix.virus|.attempt_enter
[2K[1Gvirus_prevention.fix.viru|s.attempt.enter
[2K[1Gvirus_prevention.fix.vir|us.attempt.enter
[2K[1Gvirus_prevention.fix.vi|rus.attempt.enter
[2K[1Gvirus_prevention.fix.v|irus.attempt.enter
[2K[1Gvirus_prevention.fix.|virus.attempt.enter

这不是我要找的。我所要求的是用什么代码替换
sys.stdout.write
行?如您所见,如果我选择放置
sys.stdout.write
而不是打印,则会在开始时删除多余的文本。

看起来您需要对控制台进行更多控制。您有Python 3的解决方案吗?看起来您需要对控制台进行更多的控制。您有针对Python3的解决方案吗?