我正在尝试覆盖Python2.7中的打印行
from __future__ import print_function
import time
print('This is the first statement', end='\r')
time.sleep(1)
print('This is the Second Statement')
由此,我期望第一行打印,然后一秒钟后,第二个打印行,覆盖第一行。
执行代码时得到的只是脚本运行后一秒的第二个打印行。这里发生了什么?如何让第一个语句出现在第二个语句之前,然后覆盖?
最佳答案
使用sys.stdout.flush
:
from __future__ import print_function
import time
import sys
print('This is the first statement', end='\r')
sys.stdout.flush()
time.sleep(1)
print('This is the Second Statement')
关于python-2.7 - Python Print 语句不会用\r 覆盖前一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27891519/