How to overwrite the previous print to stdout in python?

后端 未结 16 1661
别那么骄傲
别那么骄傲 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:27

    Suppress the newline and print \r.

    print 1,
    print '\r2'
    

    or write to stdout:

    sys.stdout.write('1')
    sys.stdout.write('\r2')
    

提交回复
热议问题