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

后端 未结 2 628
北海茫月
北海茫月 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:33

    You can pass end='\r' to the print() function. '\r' represents a carriage return in Python. In Python 3 for example:

    import time
    for i in range(10):
        print('Update %d' % i, end='\r')
        time.sleep(5)
    

    Here time.sleep(5) is just used to force a delay between prints. Otherwise, even though everything is being printed and then overwritten, it will happen so fast that you will only see the final value 'Update 9'. In your code, it sounds like there is a natural delay while some processes is running, using time.sleep() will not be necessary.

提交回复
热议问题