1 tqdm簡介
tqdm用於顯示進度條
簡單使用如下
from tqdm import tqdm
for i in tqdm(range(10)):
time.sleep(.5)
100%|█████████████████████| 10/10 [00:05<00:00, 1.98it/s]
2 安裝及導入
pip install tqdm
from tqdm import tqdm
# 終端輸入以下命令,驗證tqdm是否正確安裝
pip show tqdm
# 正確安裝,顯示:
Name: tqdm
Version: 4.62.3
Summary: Fast, Extensible Progress Meter
Home-page: https://tqdm.github.io
Author:
Author-email:
License: MPLv2.0, MIT Licences
Location: /opt/anaconda3/envs/NLP/lib/python3.8/site-packages
Requires:
Required-by:
注意:必須通過from
的方式進行導入,否則無效
3 自定義進度條
3.1 tqdm常用參數
iterable
(iterable) : 可迭代的對象
desc
(str) : 進度條描述
total
(int or float) : 迭代次數
leave
(bool) : 執行完畢時,是否讓進度條消失
file
(io.TextIOWrapper or io.StringIO) : 指定將進度消息輸出到何處
ncols
(int) : 進度條的長度
ascii
(bool or str) : 進度條內容顯示方式,True顯示為數字,False為圖標塊
bar_format
(str) : 進度條文字信息自定義
postfix
(dict or *) : 進度條后綴信息
color
(str) : 進度條顏色
3.2 復雜參數bar_formmat的使用方法
默認格式:'{l_bar}{bar}{r_bar}'
其具體為:100%|███████████| 10/10 [00:05<00:00, 1.98it/s]
l_bar
:進度條左邊的文字
bar
: 進度條圖形部分
r_bar
: 進度條右邊的文字
一些參數:
percentage:百分比
n_fmt:當前數
total_fmt:總數
elapsed:消耗的時間
remaining:剩余時間
rate_fmt:速率
postifx:后綴字典描述
desc、postfix默認為空;
例如:
for i in tqdm(range(10), bar_format='{desc}|{bar}|{percentage:3.0f}%'):
time.sleep(.5)
|█████████████████████|100%
4 應用
(未完成,待補充)