[英]How to print characters without a new line, inside a while loop, and with sleep?
I just realised, I can't print a character, on the same line with a delay.我刚刚意识到,我不能在同一行延迟打印一个字符。
import sys
import time
while True:
sys.stdout.write('.')
time.sleep(3.0)
I can only see the result after I break the loop with CTRL+C
.我只能在用
CTRL+C
打破循环后才能看到结果。 Is there a solution for that?有解决方案吗?
I would like to see each point being printed .....
, with a delay.我想看到每个点都被打印出来
.....
,有延迟。
Output is probably buffered, it would eventually output to the terminal after the buffer was filled, but if you want it to be shown immediately you should explicitly flush output: Output 可能是缓冲的,它最终会在缓冲区被填充后 output 到终端,但如果你希望它立即显示,你应该明确地刷新 Z78E6221F6393D1356681DB398F14CE6D:
sys.stdout.flush()
in your loop在你的循环中
You can do this in one command in python 3:您可以在 python 3 中的一个命令中执行此操作:
print(".", end='', file=sys.stdout, flush=True)
here is a sample how it can be done.这是一个如何完成的示例。 print() has a parameter that can be set like:
flush=True
: print() 有一个可以设置为的参数:
flush=True
:
from time import sleep
count = 0
while count < 20:
print('.', end='', flush=True) # prints all in one line
#print('.', flush=True) # prints on separate lines
sleep(1)
count += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.