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

2024-10-06 09:30:29 发布

您现在位置: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.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而不是打印,那么它会在开始时去掉多余的文本


Tags: timestdoutsyssleepfixwriteusvi