tqdm使用及自定义方法


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 应用

(未完成,待补充)

5 参考文献

https://www.cnblogs.com/softlin/p/13339766.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM