Python进度条的使用


Posted in Python onMay 17, 2021

在使用Python处理比较耗时操作的时候,为了便于观察处理进度,就需要通过进度条将处理情况进行可视化展示,以便我们能够及时了解情况。这对于第三方库非常丰富的Python来说,并不是什么难事。

tqdm就能非常完美的支持和解决这个问题,它是一个快速、扩展性强的进度条工具库。用户只需要封装任意的迭代器 tqdm(iterator),就能在 Python 长循环中添加一个进度提示信息。

官网:

https://github.com/tqdm/tqdm

安装:

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)

Python进度条的使用

【例子】trange(N)tqdm(range(N))的一种简单写法

import time
from tqdm import tqdm, trange

for i in trange(100):
    time.sleep(0.05)

Python进度条的使用

【例子】循环外的实例化允许手动控制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)

Python进度条的使用

【例子】

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)

Python进度条的使用

基于手动进行更新

【例子】使用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)

Python进度条的使用

如果提供了可选变量total(或带有len()的iterable),则会显示预测统计信息。

with也是可选的(可以将tqdm()赋值给变量,但在这种情况下,不要忘记在结尾处delclose()

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()

Python进度条的使用

tqdm模块参数说明

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()函数:用于设置/修改进度条的说明。
  • set_postfix()函数:用于设置/修改后缀(附加统计信息)。
  • update()函数:手动更新进度条。
  • close()函数:清除并关闭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:总的项目数。
  • leave:bool值,迭代完成后是否保留进度条。
  • file:输出指向位置,默认是终端, 一般不需要设置。
  • ncols:调整进度条宽度,默认是根据环境自动调节长度,如果设置为0,就没有进度条,只有输出的信息。
  • unit:描述处理项目的文字,默认是'it',例如: 100 it/s,处理照片的话设置为'img' ,则为 100 img/s。
  • unit_scale:自动根据国际标准进行项目处理速度单位的换算,例如 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)

Python进度条的使用

tqdm源自阿拉伯语单词taqaddum,意思是“progress(进展)”,是python中一个快速、扩展性强的进度条工具库,能让我们了解代码的运行进度,也能让我们的运行结果看起来显得更加美观而又高大上!! 喜欢的小伙伴赶紧用起来吧!!

到此这篇关于Python进度条的使用的文章就介绍到这了,更多相关Python进度条内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python计数排序和基数排序算法实例
Apr 25 Python
python中的__init__ 、__new__、__call__小结
Apr 25 Python
python实现批量修改文件名代码
Sep 10 Python
python写一个md5解密器示例
Feb 23 Python
Python中的并发处理之asyncio包使用的详解
Apr 03 Python
Python3之读取连接过的网络并定位的方法
Apr 22 Python
让Django支持Sql Server作后端数据库的方法
May 29 Python
python调用tcpdump抓包过滤的方法
Jul 18 Python
Python设计模式之桥接模式原理与用法实例分析
Jan 10 Python
使用Python批量修改文件名的代码实例
Jan 24 Python
如何利用python进行时间序列分析
Aug 04 Python
python 对一幅灰度图像进行直方图均衡化
Oct 27 Python
Python包管理工具pip的15 个使用小技巧
Python中json.dumps()函数的使用解析
May 17 #Python
Python中threading库实现线程锁与释放锁
Python中Cookies导出某站用户数据的方法
May 17 #Python
Python 高级库15 个让新手爱不释手(推荐)
Python带你从浅入深探究Tuple(基础篇)
May 15 #Python
Python中zipfile压缩包模块的使用
You might like
php 攻击方法之谈php+mysql注射语句构造
2009/10/30 PHP
php利用curl抓取新浪微博内容示例
2014/04/27 PHP
PHP实现AES256加密算法实例
2014/09/22 PHP
PHP实现二维数组(或多维数组)转换成一维数组的常见方法总结
2019/12/04 PHP
PHP加MySQL消息队列深入理解
2021/02/27 PHP
修改jquery.lazyload.js实现页面延迟载入
2010/12/22 Javascript
jQuery插件开发全解析
2012/10/10 Javascript
connect中间件session、cookie的使用方法分享
2014/06/17 Javascript
JavaScript中操作Mysql数据库实例
2015/04/02 Javascript
js显示当前日期时间和星期几
2015/10/22 Javascript
javascript仿京东导航左侧分类导航下拉菜单效果
2020/11/25 Javascript
JS提示:Uncaught SyntaxError:Unexpected token ) 错误的解决方法
2016/08/19 Javascript
bootstrap jquery dataTable 异步ajax刷新表格数据的实现方法
2017/02/10 Javascript
浅谈关于.vue文件中style的scoped属性
2017/08/19 Javascript
Vue多组件仓库开发与发布详解
2019/02/28 Javascript
原生JS封装拖动验证滑块的实现代码示例
2020/06/01 Javascript
python打开url并按指定块读取网页内容的方法
2015/04/29 Python
python 常用的基础函数
2018/07/10 Python
Python3.5内置模块之random模块用法实例分析
2019/04/26 Python
详解Python并发编程之创建多线程的几种方法
2019/08/23 Python
python解释器spython使用及原理解析
2019/08/24 Python
python 密码学示例——凯撒密码的实现
2020/09/21 Python
教你如何用python操作摄像头以及对视频流的处理
2020/10/12 Python
python drf各类组件的用法和作用
2021/01/12 Python
matplotlib对象拾取事件处理的实现
2021/01/14 Python
Boden美国官网:英伦原创时装品牌
2017/07/03 全球购物
介绍一下write命令
2014/08/10 面试题
最新党员的自我评价分享
2013/11/04 职场文书
大学生见习报告总结
2014/11/04 职场文书
2014年卫生保健工作总结
2014/12/08 职场文书
2014年副班长工作总结
2014/12/10 职场文书
和谐家庭事迹材料
2014/12/20 职场文书
详解JAVA中的OPTIONAL
2021/06/14 Java/Android
React实现动效弹窗组件
2021/06/21 Javascript
交互式可视化js库gojs使用介绍及技巧
2022/02/18 Javascript
sentinel支持的redis高可用集群配置详解
2022/04/01 Redis