【发布时间】:2013-05-07 15:01:25
【问题描述】:
test_count = 0
while test_count <= 100:
print test_count
test_count +=1
目前此计数器正在下一行打印,但我正在寻找将其覆盖在“0”上的方法。
【问题讨论】:
标签: python python-2.7 python-3.x
test_count = 0
while test_count <= 100:
print test_count
test_count +=1
目前此计数器正在下一行打印,但我正在寻找将其覆盖在“0”上的方法。
【问题讨论】:
标签: python python-2.7 python-3.x
在sys.stdout.flush 中使用\r 回车符。
import sys
import time # for invoking time.sleep(n_seconds) inside loop
counter = 0
while counter <= 100:
time.sleep(1)
counter += 1
sys.stdout.write("\rTesting (%ss elapsed)" % counter)
sys.stdout.flush()
【讨论】:
使用\r 并在您的打印语句末尾添加, 以不自动写入换行符,如下面的代码所示。
另外,请参阅python-progressbar 库,了解一些不错的进度条文本实现。
import time # Added to demonstrate effect
test_count = 0
while test_count <= 100:
print "\r%3d" % test_count,
time.sleep(0.1)
test_count +=1
【讨论】:
您应该包括以下声明: 首先在文件开头导入'os'模块:
import os
然后在循环末尾添加:
os.system('cls')
希望对你有所帮助:)
【讨论】: