Python 进度条模块tqdm
1.简介
在处理大规模数据或长时间运行的任务时,了解任务的进度对于用户体验和调试来说非常重要。tqdm 是一个用于显示进度条的 Python 库,它能将任务的进度信息直观地展示出来。
无论是遍历一个大型列表、处理批量数据,还是下载文件,tqdm 都能轻松实现进度条显示,并且与 Python 的标准库和许多第三方库无缝集成。本文将详细介绍 tqdm 库,
包括其安装方法、主要特性、基本和高级功能,以及实际应用场景,帮助全面了解并掌握该库的使用。
可以通过 pip 直接安装 tqdm:
pip install tqdm
2.特性
易于使用:只需添加一行代码即可在循环中显示进度条。
灵活性:支持多种进度条样式和自定义配置。
集成性:与 Python 的标准库(如 time、itertools)和许多第三方库(如 pandas、requests)无缝集成。
多平台支持:兼容 Linux、Windows 和 macOS 系统。
高性能:对性能影响较小,适用于大规模数据处理任务。
3.基本功能
基本用法
1.在遍历一个列表时使用 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]
2.与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]
3.自定义进度条描述
可以自定义进度条的描述文字:
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]
4.高级功能
1.嵌套进度条
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]
2.与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]
3.与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]
4.自定义进度条样式
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
5.实际应用场景
1.数据处理与分析
在数据处理与分析中,通过 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]
2.机器学习模型训练
在机器学习模型训练过程中,通过 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]
3.文件下载与上传
在文件下载与上传过程中,通过 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]
抄自于:https://www.toutiao.com/article/7390572592857416231
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少