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)
相关推荐
麦麦大数据18 小时前
F039 python五种算法美食推荐可视化大数据系统vue+flask前后端分离架构
python·算法·vue·推荐算法·美食·五种算法
喆星时瑜18 小时前
ComfyUI本地部署Stable Diffusion:核心组件(Python、PyTorch、CUDA)版本与显卡配置全指南
pytorch·python·stable diffusion
大佬,救命!!!18 小时前
定时打印的练习整理
linux·服务器·python·学习笔记·学习方法·定时发送
南棱笑笑生18 小时前
20251028在荣品RD-RK3588-MID开发板的Android13系统下解决关机的时候最近打开的应用不关的问题
开发语言·python·rockchip
红树林0719 小时前
BeautifulSoup 的页面中需要获取某个元素的 xpath 路径
前端·python·网络爬虫·beautifulsoup
jieyu111919 小时前
Python 实战:Web 漏洞 Python POC 代码及原理详解(1)
python·web安全
QQLOVEYY19 小时前
Python和PyCharm的安装教程
python·pycharm
想名字好难啊竟然不止我一个20 小时前
清除 Pip 缓存, 释放磁盘空间
python·缓存·pip
Eiceblue20 小时前
Python 快速提取扫描件 PDF 中的文本:OCR 实操教程
vscode·python·ocr·1024程序员节
APIshop20 小时前
淘宝/天猫 API 接口深度解析:商品详情获取与按图搜索商品(拍立淘)实战指南
python·1024程序员节