tqdm,一个高级的 Python 库!(python tmx)
off999 2024-09-14 07:16 31 浏览 0 评论
大家好,今天为大家分享一个高级的 Python 库 - tqdm。
Github地址:https://github.com/tqdm/tqdm
在处理大规模数据或长时间运行的任务时,了解任务的进度对于用户体验和调试来说非常重要。tqdm 是一个用于显示进度条的 Python 库,它能将任务的进度信息直观地展示出来。无论是遍历一个大型列表、处理批量数据,还是下载文件,tqdm 都能轻松实现进度条显示,并且与 Python 的标准库和许多第三方库无缝集成。本文将详细介绍 tqdm 库,包括其安装方法、主要特性、基本和高级功能,以及实际应用场景,帮助全面了解并掌握该库的使用。
安装
要使用 tqdm 库,首先需要安装它。以下是安装步骤:
使用 pip 安装
可以通过 pip 直接安装 tqdm:
pip install tqdm
特性
- 易于使用:只需添加一行代码即可在循环中显示进度条。
- 灵活性:支持多种进度条样式和自定义配置。
- 集成性:与 Python 的标准库(如 time、itertools)和许多第三方库(如 pandas、requests)无缝集成。
- 多平台支持:兼容 Linux、Windows 和 macOS 系统。
- 高性能:对性能影响较小,适用于大规模数据处理任务。
基本功能
基本用法
在遍历一个列表时使用 tqdm 显示进度条:
from tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.1)
输出结果:
100%|██████████| 100/100 [00:10<00:00, 9.57it/s]
与enumerate结合使用
在遍历带索引的列表时使用 tqdm:
from tqdm import tqdm
import time
for i, value in enumerate(tqdm(range(100))):
time.sleep(0.1)
输出结果:
100%|██████████| 100/100 [00:10<00:00, 9.58it/s]
自定义进度条描述
可以自定义进度条的描述文字:
from tqdm import tqdm
import time
for i in tqdm(range(100), desc="Processing"):
time.sleep(0.1)
输出结果:
Processing: 100%|██████████| 100/100 [00:10<00:00, 9.60it/s]
高级功能
嵌套进度条
tqdm 支持嵌套进度条,适用于多层循环的任务:
from tqdm import tqdm
import time
for i in tqdm(range(3), desc="Outer Loop"):
for j in tqdm(range(10), desc="Inner Loop", leave=False):
time.sleep(0.1)
输出结果:
Outer Loop: 0%| | 0/3 [00:00<?, ?it/s]
Inner Loop: 0%| | 0/10 [00:00<?, ?it/s]
Inner Loop: 10%|█ | 1/10 [00:00<00:00, 9.68it/s]
Inner Loop: 20%|██ | 2/10 [00:00<00:00, 9.66it/s]
Inner Loop: 30%|███ | 3/10 [00:00<00:00, 9.60it/s]
Inner Loop: 40%|████ | 4/10 [00:00<00:00, 9.58it/s]
Inner Loop: 50%|█████ | 5/10 [00:00<00:00, 9.63it/s]
Inner Loop: 60%|██████ | 6/10 [00:00<00:00, 9.72it/s]
Inner Loop: 70%|███████ | 7/10 [00:00<00:00, 9.64it/s]
Inner Loop: 80%|████████ | 8/10 [00:00<00:00, 9.60it/s]
Inner Loop: 90%|█████████ | 9/10 [00:00<00:00, 9.69it/s]
Inner Loop: 100%|██████████| 10/10 [00:01<00:00, 9.72it/s]
Outer Loop: 33%|███▎ | 1/3 [00:01<00:02, 1.04s/it]
Inner Loop: 0%| | 0/10 [00:00<?, ?it/s]
Inner Loop: 10%|█ | 1/10 [00:00<00:00, 9.77it/s]
Inner Loop: 20%|██ | 2/10 [00:00<00:00, 9.65it/s]
Inner Loop: 30%|███ | 3/10 [00:00<00:00, 9.61it/s]
Inner Loop: 40%|████ | 4/10 [00:00<00:00, 9.58it/s]
Inner Loop: 50%|█████ | 5/10 [00:00<00:00, 9.59it/s]
Inner Loop: 60%|██████ | 6/10 [00:00<00:00, 9.57it/s]
Inner Loop: 70%|███████ | 7/10 [00:00<00:00, 9.59it/s]
Inner Loop: 80%|████████ | 8/10 [00:00<00:00, 9.55it/s]
Inner Loop: 90%|█████████ | 9/10 [00:00<00:00, 9.55it/s]
Inner Loop: 100%|██████████| 10/10 [00:01<00:00, 9.56it/s]
Outer Loop: 67%|██████▋ | 2/3 [00:02<00:01, 1.04s/it]
Inner Loop: 0%| | 0/10 [00:00<?, ?it/s]
Inner Loop: 10%|█ | 1/10 [00:00<00:00, 9.95it/s]
Inner Loop: 20%|██ | 2/10 [00:00<00:00, 9.70it/s]
Inner Loop: 30%|███ | 3/10 [00:00<00:00, 9.71it/s]
Inner Loop: 40%|████ | 4/10 [00:00<00:00, 9.63it/s]
Inner Loop: 50%|█████ | 5/10 [00:00<00:00, 9.60it/s]
Inner Loop: 60%|██████ | 6/10 [00:00<00:00, 9.66it/s]
Inner Loop: 70%|███████ | 7/10 [00:00<00:00, 9.62it/s]
Inner Loop: 80%|████████ | 8/10 [00:00<00:00, 9.58it/s]
Inner Loop: 90%|█████████ | 9/10 [00:00<00:00, 9.68it/s]
Inner Loop: 100%|██████████| 10/10 [00:01<00:00, 9.67it/s]
Outer Loop: 100%|██████████| 3/3 [00:03<00:00, 1.04s/it]
与pandas结合使用
tqdm 可以与 pandas 无缝集成,显示 pandas 操作的进度条:
import pandas as pd
from tqdm import tqdm
tqdm.pandas()
df = pd.DataFrame({"a": range(1000)})
df.progress_apply(lambda x: x ** 2)
输出结果:
100%|██████████| 1/1 [00:00<00:00, 622.21it/s]
与requests结合使用
tqdm 可以与 requests 结合使用,显示文件下载的进度条:
import requests
from tqdm import tqdm
url = 'https://example.com/largefile.zip'
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))
with open('largefile.zip', 'wb') as file, tqdm(
desc='Downloading',
total=total_size,
unit='B',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in response.iter_content(chunk_size=1024):
file.write(data)
bar.update(len(data))
输出结果:
Downloading: 1.23kB [00:00, 509kB/s]
自定义进度条样式
tqdm 允许用户自定义进度条的样式:
from tqdm import tqdm
import time
for i in tqdm(range(100), bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}"):
time.sleep(0.1)
输出结果:
100%|██████████| 100/100
实际应用场景
数据处理与分析
在数据处理与分析中,通过 tqdm 显示数据处理的进度,提升用户体验。
from tqdm import tqdm
import pandas as pd
# 加载数据
df = pd.read_csv('large_dataset.csv')
# 数据处理
for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="Processing Data"):
# 进行一些数据处理操作
pass
输出结果:
Processing Data: 100%|██████████| 242/242 [00:00<00:00, 20854.32it/s]
机器学习模型训练
在机器学习模型训练过程中,通过 tqdm 显示训练进度,方便监控和调试。
from tqdm import tqdm
import time
epochs = 2
batches = 5
for epoch in tqdm(range(epochs), desc="Epochs"):
for batch in tqdm(range(batches), desc="Batches", leave=False):
# 模拟训练过程
time.sleep(0.1)
输出结果:
Epochs: 0%| | 0/2 [00:00<?, ?it/s]
Batches: 0%| | 0/5 [00:00<?, ?it/s]
Batches: 20%|██ | 1/5 [00:00<00:00, 9.84it/s]
Batches: 40%|████ | 2/5 [00:00<00:00, 9.77it/s]
Batches: 60%|██████ | 3/5 [00:00<00:00, 9.75it/s]
Batches: 80%|████████ | 4/5 [00:00<00:00, 9.69it/s]
Batches: 100%|██████████| 5/5 [00:00<00:00, 9.66it/s]
Epochs: 50%|█████ | 1/2 [00:00<00:00, 1.94it/s]
Batches: 0%| | 0/5 [00:00<?, ?it/s]
Batches: 20%|██ | 1/5 [00:00<00:00, 9.74it/s]
Batches: 40%|████ | 2/5 [00:00<00:00, 9.78it/s]
Batches: 60%|██████ | 3/5 [00:00<00:00, 9.72it/s]
Batches: 80%|████████ | 4/5 [00:00<00:00, 9.65it/s]
Batches: 100%|██████████| 5/5 [00:00<00:00, 9.58it/s]
Epochs: 100%|██████████| 2/2 [00:01<00:00, 1.93it/s]
文件下载与上传
在文件下载与上传过程中,通过 tqdm 显示进度条,提升用户体验。
import requests
from tqdm import tqdm
url = 'https://example.com/largefile.zip'
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))
with open('largefile.zip', 'wb') as file, tqdm(
desc='Downloading',
total=total_size,
unit='B',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in response.iter_content(chunk_size=1024):
file.write(data)
bar.update(len(data))
输出结果:
Downloading: 1.23kB [00:00, 1.21MB/s]
总结
tqdm 库是一个功能强大且易于使用的进度条显示工具,能够帮助开发者在处理大规模数据或长时间运行的任务时直观地了解任务进度。通过支持易于使用、灵活性、集成性、多平台支持和高性能,tqdm 提供了强大的功能和灵活的扩展能力。本文详细介绍了 tqdm 库的安装方法、主要特性、基本和高级功能,以及实际应用场景。希望本文能帮助大家全面掌握 tqdm 库的使用,并在实际项目中发挥其优势。无论是在数据处理与分析、机器学习模型训练还是文件下载与上传中,tqdm 库都将是一个得力的工具。
相关推荐
- 面试官:来,讲一下枚举类型在开发时中实际应用场景!
-
一.基本介绍枚举是JDK1.5新增的数据类型,使用枚举我们可以很好的描述一些特定的业务场景,比如一年中的春、夏、秋、冬,还有每周的周一到周天,还有各种颜色,以及可以用它来描述一些状态信息,比如错...
- 一日一技:11个基本Python技巧和窍门
-
1.两个数字的交换.x,y=10,20print(x,y)x,y=y,xprint(x,y)输出:102020102.Python字符串取反a="Ge...
- Python Enum 技巧,让代码更简洁、更安全、更易维护
-
如果你是一名Python开发人员,你很可能使用过enum.Enum来创建可读性和可维护性代码。今天发现一个强大的技巧,可以让Enum的境界更进一层,这个技巧不仅能提高可读性,还能以最小的代价增...
- Python元组编程指导教程(python元组的概念)
-
1.元组基础概念1.1什么是元组元组(Tuple)是Python中一种不可变的序列类型,用于存储多个有序的元素。元组与列表(list)类似,但元组一旦创建就不能修改(不可变),这使得元组在某些场景...
- 你可能不知道的实用 Python 功能(python有哪些用)
-
1.超越文件处理的内容管理器大多数开发人员都熟悉使用with语句进行文件操作:withopen('file.txt','r')asfile:co...
- Python 2至3.13新特性总结(python 3.10新特性)
-
以下是Python2到Python3.13的主要新特性总结,按版本分类整理:Python2到Python3的重大变化Python3是一个不向后兼容的版本,主要改进包括:pri...
- Python中for循环访问索引值的方法
-
技术背景在Python编程中,我们经常需要在循环中访问元素的索引值。例如,在处理列表、元组等可迭代对象时,除了要获取元素本身,还需要知道元素的位置。Python提供了多种方式来实现这一需求,下面将详细...
- Python enumerate核心应用解析:索引遍历的高效实践方案
-
喜欢的条友记得关注、点赞、转发、收藏,你们的支持就是我最大的动力源泉。根据GitHub代码分析统计,使用enumerate替代range(len())写法可减少38%的索引错误概率。本文通过12个生产...
- Python入门到脱坑经典案例—列表去重
-
列表去重是Python编程中常见的操作,下面我将介绍多种实现列表去重的方法,从基础到进阶,帮助初学者全面掌握这一技能。方法一:使用集合(set)去重(最简单)pythondefremove_dupl...
- Python枚举类工程实践:常量管理的标准化解决方案
-
本文通过7个生产案例,系统解析枚举类在工程实践中的应用,覆盖状态管理、配置选项、错误代码等场景,适用于Web服务开发、自动化测试及系统集成领域。一、基础概念与语法演进1.1传统常量与枚举类对比#传...
- 让Python枚举更强大!教你玩转Enum扩展
-
为什么你需要关注Enum?在日常开发中,你是否经常遇到这样的代码?ifstatus==1:print("开始处理")elifstatus==2:pri...
- Python枚举(Enum)技巧,你值得了解
-
枚举(Enum)提供了更清晰、结构化的方式来定义常量。通过为枚举添加行为、自动分配值和存储额外数据,可以提升代码的可读性、可维护性,并与数据库结合使用时,使用字符串代替数字能简化调试和查询。Pytho...
- 78行Python代码帮你复现微信撤回消息!
-
来源:悟空智能科技本文约700字,建议阅读5分钟。本文基于python的微信开源库itchat,教你如何收集私聊撤回的信息。[导读]Python曾经对我说:"时日不多,赶紧用Python"。于是看...
- 登录人人都是产品经理即可获得以下权益
-
文章介绍如何利用Cursor自动开发Playwright网页自动化脚本,实现从选题、写文、生图的全流程自动化,并将其打包成API供工作流调用,提高工作效率。虽然我前面文章介绍了很多AI工作流,但它们...
- Python常用小知识-第二弹(python常用方法总结)
-
一、Python中使用JsonPath提取字典中的值JsonPath是解析Json字符串用的,如果有一个多层嵌套的复杂字典,想要根据key和下标来批量提取value,这是比较困难的,使用jsonpat...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python人脸识别 (54)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)