单以的进度条
执行效果如图:
代码:
#文本进度条.py by@dingzeyuan
import time as t
scale = 10
print("------执行开始------")
for i in range(scale + 1):
a = '*' * i;
b = '.' * (scale - i)
c = (i / scale) * 100
print("{:^3.0f}%[{}->{}]".format(c, a, b))
t.sleep(0.1)
print("------执行结束------")
升级以后带单行动态刷新的进度条
难点:要使输出后不换行,并且光标移动到当前行的起始位置
使用转移字符 \r即可以让光标回到起始位置
end=“”不换行,在当前光标后面加一个东西,参数赋值为空字符串
代码:
#文本进度条.py by@dingzeyuan
import time as t
for i in range(101):
print("\r{:3}%".format(i), end="")
t.sleep(0.1)
终极版的文本进度条
#文本进度条.py by@dingzeyuan
import time as t
scale = 50
print("执行开始".center(scale // 2, "-"))
start = t.perf_counter()
for i in range(scale + 1):
a = '*' * i
b = '.' * (scale - i)
c = (i / scale) * 100
dur = t.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
t.sleep(0.1)
print("\n" + "执行结束".center(scale//2, '-'))