這里主要是有兩個方法:tqdm 和 progressbar
1. 首先是tqdm方法:
from time import sleep from tqdm import tqdm for i in tqdm(range(10)): # 需要循環或者多次執行的代碼 print('\n the value of i is %d ---'%i) #正常的整數值:i for ii in range(i): print(ii) sleep(0.1)
- tqdm顯示進度條解釋:注意參數不一定要是數字,可迭代對象即可
from tqdm import tqdm import time for ss in tqdm(range(10)): time.sleep(10) print('this is test for tqdm with:',ss) list_ = ['高樓','湖水','大海'] #可迭代對象即可,不一定要數字 for ss in tqdm(list_): time.sleep(10) print('this is test for tqdm with:', ss)
可以很明顯看到:
(1)想看進度,直接看百分比即可,表示完成了多少,例如80%,當然也可以看分數8/10。
(2)第一個時間表示已經執行的時間,第二個時間表示還剩多少時間。
(3)速度:s/it,表示每次迭代需要的時間。
- tqdm函數定義和部分參數
def __init__(self, iterable=None, desc=None, total=None, leave=True, file=None, 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, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, gui=False, **kwargs): """ Parameters ---------- iterable : iterable, optional Iterable to decorate with a progressbar. Leave blank to manually manage the updates. desc : str, optional Prefix for the progressbar. total : int, optional The number of expected iterations. If unspecified, len(iterable) is used if possible. As a last resort, only basic progress statistics are displayed (no ETA, no progressbar). If `gui` is True and this parameter needs subsequent updating, specify an initial arbitrary large positive integer, e.g. int(9e9). leave : bool, optional If [default: True], keeps all traces of the progressbar upon termination of iteration. ...... bar_format : str, optional Specify a custom bar string formatting. May impact performance. [default: '{l_bar}{bar}{r_bar}'], where l_bar='{desc}: {percentage:3.0f}%|' and r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}]' Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt, percentage, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, elapsed, remaining, desc, postfix. Note that a trailing ": " is automatically removed after {desc} if the latter is empty. initial : int, optional The initial counter value. Useful when restarting a progress bar [default: 0]. position : int, optional Specify the line offset to print this bar (starting from 0) Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads). postfix : dict or *, optional Specify additional stats to display at the end of the bar. Calls `set_postfix(**postfix)` if possible (dict). ......
使用示例:
list_ = ['高樓','湖水','大海'] for ss in tqdm(list_, desc='---', postfix='***'): time.sleep(10) print('this is test for tqdm with:', ss)
4. 其次是progressbar方法
from progressbar import ProgressBar, Percentage, Bar, Timer, ETA, FileTransferSpeed aa = [1,2,3,4,5,6,7,8,9] total = len(aa) def dowith_i(i): for ii in range(i): print(ii) sleep(0.1) widgets = ['當前進度: ',Percentage(), ' ', Bar('=>'),' ', Timer(), ' ', ETA(), ' ', FileTransferSpeed()] bar_object = ProgressBar(widgets=widgets, maxval=10*total).start() for i in range(total): print('\n') dowith_i(i) #做自己的任務 bar_object.update(10 * i + 1) bar_object.finish()
其中 'widgets' 參數可以自己設置。
Timer:表示經過的秒(時間)
Bar:設置進度條形狀
Percentage:顯示百分比進度
ETA:預估剩余時間
FileTransferSpeed:文件傳輸速度
參考:
https://baijiahao.baidu.com/s?id=1604219346207216803&wfr=spider&for=pc