Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置
tqdm官網(wǎng)地址:https://pypi.org/project/tqdm/
Github地址:https://github.com/tqdm/tqdm
簡(jiǎn)介
Tqdm 是一個(gè)快速,可擴(kuò)展的Python進(jìn)度條,可以在 Python 長(zhǎng)循環(huán)中添加一個(gè)進(jìn)度提示信息,用戶(hù)只需要封裝任意的迭代器 tqdm(iterator)。
總之,它是用來(lái)顯示進(jìn)度條的,很漂亮,使用很直觀(在循環(huán)體里邊加個(gè)tqdm),而且基本不影響原程序效率。名副其實(shí)的“太強(qiáng)太美”了!這樣在寫(xiě)運(yùn)行時(shí)間很長(zhǎng)的程序時(shí),是該多么舒服??!
給一張GIF圖看一下實(shí)際效果
安裝
pip install tqdm
使用
示例一
簡(jiǎn)單的demo:
# !/user/bin/env python # -*- coding:utf-8 -*- import time from tqdm import tqdm from tqdm._tqdm import trange for i in tqdm(range(100)): time.sleep(0.01)
輸出結(jié)果如下:
關(guān)于tqdm對(duì)于range的封裝
import time from tqdm import tqdm from tqdm._tqdm import trange for j in trange(100): time.sleep(0.1)
輸出結(jié)果如下(同上)
示例二:
對(duì)于任意list的使用
alist = list('letters') bar = tqdm(alist) for letter in bar: bar.set_description(f"Now get {letter}")
輸出結(jié)果如下:
傳入任意list
pbar = tqdm(["a", "b", "c", "d"]) for char in pbar: pbar.set_description("Processing %s" % char)
手動(dòng)控制更新
with tqdm(total=100) as pbar: for i in range(10): pbar.update(10) # 也可以這樣 pbar = tqdm(total=100) for i in range(10): pbar.update(10) pbar.close()
示例三:
結(jié)合pandas的使用
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0, 100, (10000000, 6))) tqdm.pandas(desc="my bar!") df.progress_apply(lambda x: x**2)
輸出結(jié)果如下:
示例四
在Shell的tqdm用法
$ time find . -name '*.py' -exec cat \{} \; | wc -l 857365 real 0m3.458s user 0m0.274s sys 0m3.325s $ time find . -name '*.py' -exec cat \{} \; | tqdm | wc -l 857366it [00:03, 246471.31it/s] 857365 real 0m3.585s user 0m0.862s sys 0m3.358s
使用的參數(shù):
$ find . -name '*.py' -exec cat \{} \; | tqdm --unit loc --unit_scale --total 857366 >> /dev/null 100%|███████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s]
備份一個(gè)目錄:
$ 7z a -bd -r backup.7z docs/ | grep Compressing | tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log 100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s]
本文參考:https://blog.csdn.net/langb2014/article/details/54798823?locationnum=8&fps=1
到此這篇關(guān)于Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置的文章就介紹到這了,更多相關(guān)Python Tqdm進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入解析神經(jīng)網(wǎng)絡(luò)從原理到實(shí)現(xiàn)
這篇文章主要介紹了深入解析神經(jīng)網(wǎng)絡(luò)從原理到實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07jupyter notebook出現(xiàn)In[*]的問(wèn)題及解決
這篇文章主要介紹了jupyter notebook出現(xiàn)In[*]的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python用faker庫(kù)批量生成假數(shù)據(jù)
這篇文章主要介紹了python用faker庫(kù)批量生成假數(shù)據(jù),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03用Python將GIF動(dòng)圖分解成多張靜態(tài)圖片
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著如何用Python將GIF動(dòng)圖分解成多張靜態(tài)圖片展開(kāi),文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06關(guān)于Django Models CharField 參數(shù)說(shuō)明
這篇文章主要介紹了關(guān)于Django Models CharField 參數(shù)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03使用python進(jìn)行文本預(yù)處理和提取特征的實(shí)例
今天小編就為大家分享一篇使用python進(jìn)行文本預(yù)處理和提取特征的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python中用于去除空格的三個(gè)函數(shù)的使用小結(jié)
這篇文章主要介紹了Python中用于去除空格的三個(gè)函數(shù)的使用小結(jié),對(duì)strip()和lstrip()和rstrip()這三個(gè)函數(shù)做了簡(jiǎn)單的講解,需要的朋友可以參考下2015-04-04