Python - how do I display an updating value on one line and not scroll down the screen

后端 未结 2 626
北海茫月
北海茫月 2021-01-13 11:07

I have a python script which receives values in real-time. My script prints the values as they are received and currently, each update is printed to the screen and the outpu

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 11:11

    In python 2.7, the print function will not allow an end parameter, and print 'Update %d\r' will also print a newline character.

    Here is the same example @elethan gave for python2.7. Note it uses the sys.stdout output stream:

    import sys
    import time
    output_stream = sys.stdout
    
    for i in xrange(10):
        output_stream.write('Update %s\r' % i)
        output_stream.flush()
        time.sleep(5)
    # Optionally add a newline if you want to keep the last update.
    output_stream.write('\n')
    

提交回复
热议问题