python progressbar设置当前进度的方法_python加入进度条:tqdm 和 progressbar

这里主要是有两个方法:tqdm 和 progressbar

1. 首先是tqdm方法:

from time importsleepfrom tqdm importtqdm

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 importtqdmimporttimefor ss in tqdm(range(10)):

time.sleep(10)print('this is test for tqdm with:',ss)

list_= ['高楼','湖水','大海'] #可迭代对象即可,不一定要数字for ss intqdm(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}

'{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 importProgressBar, Percentage, Bar, Timer, ETA, FileTransferSpeed

aa= [1,2,3,4,5,6,7,8,9]

total=len(aa)defdowith_i(i):for ii inrange(i):print(ii)

sleep(0.1)

widgets= ['当前进度:',Percentage(), ' ', Bar('=>'),' ', Timer(),' ', ETA(), ' ', FileTransferSpeed()]

bar_object= ProgressBar(widgets=widgets, maxval=10*total).start()for i inrange(total):print('\n')

dowith_i(i) #做自己的任务

bar_object.update(10 * i + 1)

bar_object.finish()

其中 'widgets' 参数可以自己设置。

Timer:表示经过的秒(时间)

Bar:设置进度条形状

Percentage:显示百分比进度

ETA:预估剩余时间

FileTransferSpeed:文件传输速度

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值