程序运行时,默认是不提供进度信息的,在迭代时若想看到程序运行速度和时间,就此时需要使用进度条信息或日志信息来展示程序进度,tqdm是python的第三方进度条库,功能强大且易于使用,今天我们来学习如何使用tqdm实时显示程序进度信息。
tqdm的安装
pip install tqdm
tqdm的使用
tqdm这个库非常强大,即提供了对外的简单接口,也提供了复杂的参数方便扩展。
tqdm基本介绍
在使用tqdm之前,首先需要对它进行做一个简单的介绍,tqdm是一个python的第三方开源库,源代码在:Github。
class tqdm():
"""
Decorate an iterable object, returning an iterator which acts exactly
like the original iterable, but prints a dynamically updating
progressbar every time a value is requested.
"""
def __init__(self, iterable=None, desc=None, total=None, leave=True,
file=None, ncols=None, mininterval=0.1,
maxinterval=10.0, miniters=None, ascii=None, disable=False,
unit='it', unit_scale=False, dynamic_ncols=False,
smoothing=0.3, bar_format=None, initial=0, position=None,
postfix=None, unit_divisor=1000):
这里简单介绍一下常用的参数:
desc=None, str类型,作为进度条说明,重要参数total=None, 预期的迭代次数 ,重要参数file=None, 输出方式,默认为sys.stderrncols=None, 进度条长度或宽度mininterval=0.1, 进度条最小的更新间隔,单位秒,默认:0.1maxinterval=10.0, 进度条最大更新间隔,单位秒,默认:10unit='it', 单位,默认it每秒迭代数bar_format=None, 进度条格式ascii, 是否使用asciipostfix, 字典形式信息,例如:速度=5
基于可迭代
基于可迭代的有两种方式,一种是基于tqdm,另一种是基于trange。
基于tqdm的可迭代用法
from tqdm import tqdm
from time import sleep
text = ""
for char in tqdm(["a", "b", "c", "d"]<

本文介绍了Python第三方库tqdm的使用,包括基本用法、参数设置、高级功能和与其他程序的搭配。通过tqdm,可以方便地在迭代过程中显示实时进度信息,提升程序的用户体验。示例中展示了如何通过tqdm和trange创建进度条,以及如何自定义进度条的样式和附加信息,如速度、下载进度等。
最低0.47元/天 解锁文章
4214

被折叠的 条评论
为什么被折叠?



