Python進度條的使用
在使用Python處理比較耗時操作的時候,為了便于觀察處理進度,就需要通過進度條將處理情況進行可視化展示,以便我們能夠及時了解情況。這對于第三方庫非常豐富的Python來說,并不是什么難事。
tqdm就能非常完美的支持和解決這個問題,它是一個快速、擴展性強的進度條工具庫。用戶只需要封裝任意的迭代器 tqdm(iterator),就能在 Python 長循環(huán)中添加一個進度提示信息。
官網(wǎng):
安裝:
pip install tqdm
基于迭代器的使用方式
【例子】使用tqdm(iterator)
import time
from tqdm import tqdm
for i in tqdm(range(100)):
time.sleep(0.05)
for i in tqdm(list('abcdefgh')):
time.sleep(0.05)
for i in tqdm(range(100), desc='Processing'):
time.sleep(0.05)

【例子】trange(N)是tqdm(range(N))的一種簡單寫法
import time
from tqdm import tqdm, trange
for i in trange(100):
time.sleep(0.05)

【例子】循環(huán)外的實例化允許手動控制tqdm()
import time
from tqdm import tqdm
pbar = tqdm(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
for i in pbar:
pbar.set_description('Processing ' + i)
time.sleep(0.2)

【例子】
import time
from tqdm import tqdm
from random import random, randint
with tqdm(range(100)) as pbar:
for i in pbar:
pbar.set_description("GEN %d" % i)
pbar.set_postfix({'loss': random(), 'gen': randint(1, 999)})
time.sleep(0.1)

基于手動進行更新
【例子】使用with語句手動控制tqdm()更新
import time
from tqdm import tqdm
with tqdm(total=200) as pbar:
pbar.set_description("Processing")
for i in range(20):
time.sleep(0.1)
pbar.update(10)

如果提供了可選變量total(或帶有len()的iterable),則會顯示預測統(tǒng)計信息。
with也是可選的(可以將tqdm()賦值給變量,但在這種情況下,不要忘記在結尾處del或close()。
import time
from tqdm import tqdm
pbar = tqdm(total=200)
pbar.set_description("Processing")
for i in range(20):
time.sleep(0.1)
pbar.update(10)
pbar.close()

tqdm模塊參數(shù)說明
class tqdm(Comparable):
"""
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 set_description(self, desc=None, refresh=True):
def set_postfix(self, ordered_dict=None, refresh=True, **kwargs):
def update(self, n=1):
def close(self):
set_description()函數(shù):用于設置/修改進度條的說明。set_postfix()函數(shù):用于設置/修改后綴(附加統(tǒng)計信息)。update()函數(shù):手動更新進度條。close()函數(shù):清除并關閉progressbar。
class tqdm(Comparable):
"""
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:調整進度條寬度,默認是根據(jù)環(huán)境自動調節(jié)長度,如果設置為0,就沒有進度條,只有輸出的信息。
- unit:描述處理項目的文字,默認是'it',例如: 100 it/s,處理照片的話設置為'img' ,則為 100 img/s。
- unit_scale:自動根據(jù)國際標準進行項目處理速度單位的換算,例如 100000 it/s >> 100k it/s。
【例子】
import time
from tqdm import tqdm
with tqdm(total=100000, desc='Example', leave=True, ncols=100, unit='B', unit_scale=True) as pbar:
for i in range(10):
time.sleep(0.5)
pbar.update(10000)

tqdm源自阿拉伯語單詞taqaddum,意思是“progress(進展)”,是python中一個快速、擴展性強的進度條工具庫,能讓我們了解代碼的運行進度,也能讓我們的運行結果看起來顯得更加美觀而又高大上??! 喜歡的小伙伴趕緊用起來吧??!
到此這篇關于Python進度條的使用的文章就介紹到這了,更多相關Python進度條內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
tensorflow之tf.record實現(xiàn)存浮點數(shù)數(shù)組
今天小編就為大家分享一篇tensorflow之tf.record實現(xiàn)存浮點數(shù)數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Flask框架運用Ajax實現(xiàn)數(shù)據(jù)交互的示例代碼
使用Ajax技術網(wǎng)頁應用能夠快速地將增量更新呈現(xiàn)在用戶界面上,而不需要重載刷新整個頁面,這使得程序能夠更快地回應用戶的操作,本文將簡單介紹使用AJAX如何實現(xiàn)前后端數(shù)據(jù)通信2022-11-11
pytorch動態(tài)神經(jīng)網(wǎng)絡(擬合)實現(xiàn)
這篇文章主要介紹了pytorch動態(tài)神經(jīng)網(wǎng)絡(擬合)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
Windows平臺Python連接sqlite3數(shù)據(jù)庫的方法分析
這篇文章主要介紹了Windows平臺Python連接sqlite3數(shù)據(jù)庫的方法,結合實例形式分析了Windows平臺安裝SQLite數(shù)據(jù)庫及創(chuàng)建、連接數(shù)據(jù)庫的實現(xiàn)方法與相關注意事項,需要的朋友可以參考下2017-07-07
Python數(shù)據(jù)分析之繪制m1-m2數(shù)據(jù)
這篇文章主要介紹了Python數(shù)據(jù)分析之繪制m1-m2數(shù)據(jù),文章基于python的相關資料展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05
Python使用Beautiful Soup實現(xiàn)解析網(wǎng)頁
在這篇文章中,我們將介紹如何使用 Python 編寫一個簡單的網(wǎng)絡爬蟲,以獲取并解析網(wǎng)頁內容。我們將使用 Beautiful Soup 庫,它是一個非常強大的庫,用于解析和操作 HTML 和 XML 文檔。讓我們開始吧2023-05-05
Python監(jiān)控服務器實用工具psutil使用解析
這篇文章主要介紹了Python監(jiān)控服務器實用工具psutil使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12

