python模块——tqdm

tqdm 模块简介

tqdm 是 Python 中一个轻量级、高性能的​​进度条显示库​​,主要用于在循环或耗时任务中实时展示执行进度,提升用户体验和程序可观测性

官方文档:https://tqdm.github.io/

tqdm 使用

安装tqdm依赖包

pip install tqdm

tqdm 参数说明

参数名 含义
iterable 可迭代对象
desc 进度条的前缀描述
total 总进度值
unit 进度单位
bar_format 进度条格式
position 进度条所在的行数,常用于多个并发任务的进度观测
leave 终端是否保留进度条
postfix 进度条的后缀描述

tqdm示例

python 复制代码
import tqdm
from tqdm import trange
import time

def main():
    """
    tqdm参数详解及示例
    """
    # 基本用法
    for i in tqdm.tqdm(range(10), desc="Basic"):
        time.sleep(0.1)

    # 参数说明及示例
    # 1. desc: 进度条描述信息
    for i in trange(10, desc="描述信息"):
        time.sleep(0.1)

    # 2. total: 总迭代次数,如果可迭代对象长度未知,可以手动指定
    for i in tqdm.tqdm(range(10), total=10, desc="指定总迭代次数"):
        time.sleep(0.1)

    # 3. leave: 是否保留进度条在完成后
    for i in tqdm.tqdm(range(10), leave=True, desc="保留进度条"):
        time.sleep(0.1)

    # 4. file: 输出流,默认为sys.stderr
    with open("output.txt", "w") as f:
        for i in tqdm.tqdm(range(10), file=f, desc="写入文件"):
            time.sleep(0.1)

    # 5. ncols: 进度条宽度
    for i in tqdm.tqdm(range(10), ncols=50, desc="自定义宽度"):
        time.sleep(0.1)

    # 6. mininterval: 最小更新间隔(秒)
    for i in tqdm.tqdm(range(10), mininterval=0.5, desc="最小更新间隔"):
        time.sleep(0.1)

    # 7. maxinterval: 最大更新间隔(秒)
    for i in tqdm.tqdm(range(10), maxinterval=1, desc="最大更新间隔"):
        time.sleep(0.1)

    # 8. miniters: 最小更新次数
    for i in tqdm.tqdm(range(10), miniters=2, desc="最小更新次数"):
        time.sleep(0.1)

    # 9. ascii: 是否使用ASCII字符集
    for i in tqdm.tqdm(range(10), ascii=True, desc="ASCII字符集"):
        time.sleep(0.1)

    # 10. disable: 是否禁用进度条
    for i in tqdm.tqdm(range(10), disable=False, desc="启用进度条"):
        time.sleep(0.1)

    # 11. unit: 单位名称
    for i in tqdm.tqdm(range(10), unit="it", unit_scale=True, desc="单位名称"):
        time.sleep(0.1)

    # 14. smoothing: 平滑因子
    for i in tqdm.tqdm(range(10), smoothing=0.3, desc="平滑因子"):
        time.sleep(0.1)

    # 15. bar_format: 自定义进度条格式
    for i in tqdm.tqdm(range(10), bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]', desc="自定义格式"):
        time.sleep(0.1)

    # 16. initial: 初始值
    for i in tqdm.tqdm(range(10), initial=5, total=15, desc="初始值"):
        time.sleep(0.1)

    # 17. position: 进度条位置
    for i in tqdm.tqdm(range(10), position=0, desc="位置0"):
        time.sleep(0.1)
    for i in tqdm.tqdm(range(10), position=1, desc="位置1"):
        time.sleep(0.1)

    # 18. postfix: 后缀信息
    for i in tqdm.tqdm(range(10), postfix="后缀信息", desc="后缀信息"):
        time.sleep(0.1)

	# 19. colour: 颜色
    for i in tqdm.tqdm(range(10), colour="green", desc="颜色"):
        time.sleep(0.1)
相关推荐
程序员杰哥30 分钟前
软件测试之功能测试
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
码界奇点41 分钟前
Python深度挖掘:openpyxl与pandas高效数据处理实战指南
开发语言·数据库·python·自动化·pandas·python3.11
Rockson42 分钟前
如何使用 JavaScript 接入实时行情 API
javascript·python·api
Billy_Zuo1 小时前
Android调用python库和方法的实现
android·开发语言·python
xulihang1 小时前
命令行调用扫描仪扫描文档
前端·python·命令行
测试19981 小时前
如何编写好的测试用例?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
Dreamsi_zh2 小时前
Python爬虫04_Requests豆瓣电影爬取
开发语言·爬虫·python
230L1_78M69Q5487H2 小时前
【机器学习】机器学习新手入门概述
人工智能·python·机器学习·scikit-learn
窗户2 小时前
有限Abel群的结构(3)
python·抽象代数·编程范式
饭来_2 小时前
Python 中使用 OpenCV 库来捕获摄像头视频流并在窗口中显示
python·opencv