Printing on the same line on a jupyter notebook

前端 未结 3 1887
-上瘾入骨i
-上瘾入骨i 2020-12-05 18:19

In python 3, we can easily print on the same line using the following script. I use this to understand the progress of my loop (how much time will be left). However, in jupy

3条回答
  •  不知归路
    2020-12-05 19:14

    The part "\r" overwrites the line, if you leave that you append to the line. Your version print(f, end='', flush=False) could work but I've read under Python 3 you need to use sys.stdout.write() and best is if you add flush command too.

    import sys
    import time
    
    for f in range(10):
        #delete "\r" to append instead of overwrite
        sys.stdout.write("\r" + str(f))
        sys.stdout.flush()
        time.sleep(10)
    

    The stdout.flush is required on some systems or you won't get any output

提交回复
热议问题