How to overwrite the previous print to stdout in python?

后端 未结 16 1433
别那么骄傲
别那么骄傲 2020-11-22 09:46

If I had the following code:

for x in range(10):
     print x

I would get the output of

1
2
etc..

What I

16条回答
  •  忘了有多久
    2020-11-22 10:33

    I'm a bit surprised nobody is using the backspace character. Here's one that uses it.

    import sys
    import time
    
    secs = 1000
    
    while True:
        time.sleep(1)  #wait for a full second to pass before assigning a second
        secs += 1  #acknowledge a second has passed
    
        sys.stdout.write(str(secs))
    
        for i in range(len(str(secs))):
            sys.stdout.write('\b')
    

提交回复
热议问题