[英]How to replace text on python shell
I would like to make a program that will print out number values. 我想制作一个可以打印数字值的程序。 I want it to display the number, then replace it with another number.
我希望它显示数字,然后将其替换为另一个数字。 A little like this:
有点像这样:
val = 0
for looper in range(1, 10):
val += 1
print (val)
#Code to replace old number to new number
Thanks for helping! 感谢您的帮助!
If you want in-place update of the number, you can use \\r. 如果要就地更新号码,可以使用\\ r。
import sys
val = 0
for looper in range(1, 10):
val += 1
sys.stdout.write("\r%d" % (val))
sys.stdout.flush()
time.sleep(1) # sleep added so that you can see the effect
Seems like you're looking for something to replace output in sys.stdout
. 似乎您正在寻找替代
sys.stdout
输出的东西。
This is limited without using external libraries but you can do something like this: 这是有限的,无需使用外部库,但是您可以执行以下操作:
import sys
import time
for number in xrange(10):
# put number in 'stdout'
sys.stdout.write(str(number))
# empty stdout (by default it waits for a new line)
sys.stdout.flush()
# display the number for some time
time.sleep(1)
# go back to beginning of input to override existing output
sys.stdout.write('\r')
我认为您正在将回车符与time.sleep(ms)一起使用“ \\ r”
On Python 3, @Teemu Kurppa's answer could be written as: 在Python 3上, @ Teemu Kurppa的答案可以写成:
import time
for i in range(1, 11):
print(i, end='\r', flush=True)
time.sleep(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.