Python实现进度条的4种方式
发布时间:2020-03-02 03:30:46
来源:51CTO
阅读:331
这里只列举了部分方法,其他方法或python库暂时还没使用到
1.不用库,直接打印:
代码样例:
import time
#demo1
def process_bar(percent, start_str='', end_str='', total_length=0):
bar = ''.join(["\033[31m%s\033[0m"%' '] int(percent total_length)) + ''
bar = '\r' + start_str + bar.ljust(total_length) + ' {:0>4.1f}%|'.format(percent*100) + end_str
print(bar, end='', flush=True)
for i in range(101):
time.sleep(0.1)
end_str = '100%'
process_bar(i/100, start_str='', end_str=end_str, total_length=15)
#demo2
for i in range(0, 101, 2):
time.sleep(0.1)
num = i // 2
if i == 100:
process = "\r[%3s%%]: |%-50s|\n" % (i, '|' * num)
else:
process = "\r[%3s%%]: |%-50s|" % (i, '|' * num)
print(process, end='', flush=True)**
2.不用库,直接打印:
代码样例专业术语:
import sys, time
print("正在下载......")
for i in range(11):
if i != 10:
sys.stdout.write("==")
else:
sys.stdout.write("== " + str(i*10)+"%/100%")
sys.stdout.flush()
time.sleep(0.2)
print("\n" + "下载完成")
注:
sys.stdout.write()方法跟print()方法的区别是 前者打印不换行,后者换行。
sys.stdout.flush()方法是立即刷新输出的内容
3.tqdm库
使用样例:
from time import sleep
from tqdm import tqdm
for i in tqdm(range(20)):
sleep(0.5)
4.progressbar
安装:
pip3 install progressbar2
使用样例:
import time
from progressbar import *
progress = ProgressBar()
for i in progress(range(1000)):
time.sleep(0.01)