Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator)
可以用trange替代range
1 | # 方法1: |
结果:1
2100%|██████████| 100/100 [00:01<00:00, 97.03it/s]
100%|██████████| 100/100 [00:01<00:00, 97.30it/s]
可以为进度条设置描述
相当于下面的"Processing %s"
是额外加载每一行的1
2
3
4
5
6
7
8import time
from tqdm import tqdm
pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
pbar.set_description("Processing %s" % char)
time.sleep(1)
#pbar.close() 不需要
结果:1
Processing d: 100%|██████████| 4/4 [00:04<00:00, 1.00s/it]
使用update()控制更新频率
1 | import time |
结果:1
2100%|██████████| 200/200 [00:01<00:00, 98.18it/s]
100%|██████████| 200/200 [00:01<00:00, 98.02it/s]
用了with就不用在结束时使用close,没用with的情况下还要额外使用close()的。
参考链接
1.https://github.com/tqdm/tqdm
2.https://www.jianshu.com/p/21cf48be6bf6