python-2.7 - Python Print 语句不会用\r 覆盖前一行

标签 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')

由此,我期望第一行打印,然后一秒钟后,第二个打印行,覆盖第一行。

执行代码时得到的只是脚本运行后一秒的第二个打印行。这里发生了什么?如何让第一个语句出现在第二个语句之前,然后覆盖?

最佳答案

使用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/

相关文章:

python-2.7 - Python 2.7 pip install lxml UnicodeDecodeError

mysql - python 中的 SQL % 语句返回错误

swift - 尝试打印两个值时出错

java - SNMP:打印页数的通用 OID

python - 列表索引越界,原因不是很明显 numpy

Python 没有从 subprocess.check_call 获取原始二进制文件

python - 在哪里可以找到 Azure Cosmos DB 中的 masterekey 访问的访问日志

html - 如何编写友好的 xhtml 和 css 打印机?如何使用 print css 节省打印机墨水?

silverlight - 在 Silverlight 打印中捕获打印对话框的取消

windows - 在 Windows 服务中打印