python進度條庫tqdm的基本操作方法
更新時間:2022年03月23日 08:51:59 作者:周兵
這篇文章主要介紹了python進度條庫tqdm的基本操作方法,tqdm實時輸出處理進度而且占用的CPU資源非常少,支持windows、Linux、mac等系統(tǒng),支持循環(huán)處理、多進程、遞歸處理、還可以結合linux的命令來查看處理情況等優(yōu)點,下面對其更多內(nèi)容詳細介紹,需要的朋友可以參考一下
1.tqdm模塊是python進度條庫, 主要分為兩種運行模式
1.1基于迭代對象運行: tqdm(iterator)
import time
from tqdm import tqdm, trange
#trange(i)是tqdm(range(i))的一種簡單寫法
for i in trange(100):
time.sleep(0.05)
for i in tqdm(range(100), desc='Processing'):
time.sleep(0.05)
dic = ['a', 'b', 'c', 'd', 'e']
pbar = tqdm(dic)
for i in pbar:
pbar.set_description('Processing '+i)
time.sleep(0.2)
100%|██████████| 100/100 [00:06<00:00, 16.04it/s]
Processing: 100%|██████████| 100/100 [00:06<00:00, 16.05it/s]
Processing e: 100%|██████████| 5/5 [00:01<00:00, 4.69it/s]1.2手動進行更新
import time
from tqdm import tqdm
with tqdm(total=200) as pbar:
pbar.set_description('Processing:')
# total表示總的項目, 循環(huán)的次數(shù)20*10(每次更新數(shù)目) = 200(total)
for i in range(20):
# 進行動作, 這里是過0.1s
time.sleep(0.1)
# 進行進度更新, 這里設置10個
pbar.update(10)
Processing:: 100%|██████████| 200/200 [00:02<00:00, 91.94it/s]2.tqdm模塊參數(shù)說明
class tqdm(object):
"""
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=False,
file=sys.stderr, 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, nested=False,
bar_format=None, initial=0, gui=False):iterable: 可迭代的對象, 在手動更新時不需要進行設置desc: 字符串, 左邊進度條描述文字total: 總的項目數(shù)leave: bool值, 迭代完成后是否保留進度條file: 輸出指向位置, 默認是終端, 一般不需要設置ncols: 調(diào)整進度條寬度, 默認是根據(jù)環(huán)境自動調(diào)節(jié)長度, 如果設置為0, 就沒有進度條, 只有輸出的信息unit: 描述處理項目的文字, 默認是'it', 例如: 100 it/s, 處理照片的話設置為'img' ,則為 100 img/sunit_scale: 自動根據(jù)國際標準進行項目處理速度單位的換算, 例如 100000 it/s >> 100k it/s
3.下面是實例展示
import time
from tqdm import tqdm
# 發(fā)呆0.5s
def action():
time.sleep(0.5)
with tqdm(total=100000, desc='Example', leave=True, ncols=100, unit='B', unit_scale=True) as pbar:
for i in range(10):
# 發(fā)呆0.5秒
action()
# 更新發(fā)呆進度
pbar.update(10000)
Example: 100%|███████████████████████████████████████████████████| 100k/100k [00:05<00:00, 19.6kB/s]到此這篇關于python進度條庫tqdm的基本操作方法的文章就介紹到這了,更多相關python進度條庫tqdm內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python創(chuàng)建普通菜單示例【基于win32ui模塊】
這篇文章主要介紹了Python創(chuàng)建普通菜單,結合實例形式分析了Python基于win32ui模塊創(chuàng)建普通菜單及添加菜單項的相關操作技巧,并附帶說明了win32ui模塊的安裝命令,需要的朋友可以參考下2018-05-05
python3.8+django2+celery5.2.7環(huán)境準備(python測試開發(fā)django)
這篇文章主要介紹了python測試開發(fā)django之python3.8+django2+celery5.2.7環(huán)境準備工作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

