Python 2.7 Python打印语句不使用';无法用覆盖上一行\r\n
我正在尝试覆盖Python2.7中的打印行Python 2.7 Python打印语句不使用';无法用覆盖上一行\r\n,python-2.7,printing,Python 2.7,Printing,我正在尝试覆盖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') 从这一点上,我希望第一行打印出来,然后一秒钟后,第二行打印出来,覆盖第一行 当我执行代码时,我得到的只是脚本运行一秒钟后打印的第二行。这是怎么回事?如何使第
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')
所需冲洗背后的理论是什么?为什么其他打印操作不需要flush()?数据被缓冲调用flush将强制操作系统将其写入终端。影响它的是时间、睡眠。