python - Python sys.stdout.flush() 似乎不起作用
问题描述
我想通过反复覆盖 print 函数来查看我的 for 循环的进度。我希望每次循环进行时都会覆盖打印的行。发生的事情是,在每个循环之后,都会在前一个打印行的正下方打印一个新行。这是通过从打印行中删除 \n 来解决的,因为 \n 会导致每次覆盖一个新的空行,而不是覆盖第一个打印行。
import sys
import time
count1=0
x=10
for i in range(x):
count1+=1
sys.stdout.write("\r{} out of {}...\n".format(count1, x))
sys.stdout.flush()
time.sleep(.1)
我在 Jupter Notebook 4.3.1 中使用 Python 3.6(64 位)。在 Windows 10 上。
我已经查看了 Stack Overflow 上发布的几个问题,但我还没有能够解决它。
谢谢!
解决方案
删除创建新行的“\n”。
sys.stdout.write("\r{} out of {}...".format(count1, x))
推荐阅读
- spring-boot - Spring Data JDBC UnsatisfiedDependencyException
- ios - 如何在swift中用变音符号分割字符串,如“ä”
- python - 如何计算神经网络中预测变量的相对重要性
- json - 尝试映射 JSON 字典 child_items 数组(如果存在)
- python - 无法使用 * with 导入
- flutter - 容器的顶部是彩色的,但其余部分是白色的。如何在颤动中做到这一点?
- c++ - c ++如何从文本文件中的特定行读取和写入?
- android - 如何使用 RxJava 2 Android 对来自传感器的微秒内实时数据进行计数、比较和存储在数组中
- c - 需要帮助弄清楚为什么循环中的这个 C 管道会因两次以上的迭代而失败
- random - 您可以对列表的分区进行多少组合以最小化重复?