python进度条显示-tqmd模块的实现示例

Tqdm 是一个快速,可扩展的Python进度条,可以在 Python 长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator)。
总之,它是用来显示进度条的,很漂亮,使用很直观(在循环体里边加个tqdm),而且基本不影响原程序效率。名副其实的“太强太美”了!这样在写运行时间很长的程序时,是该多么舒服啊!
tqdm官网地址:https://pypi.org/project/tqdm/
Github地址:https://github.com/tqdm/tqdm

安装
anaconda 是自动集成的
如果导入不存在,直接pip

pip install tqmd

参数
#参数介绍iterable=None,desc=None, 传入str类型,作为进度条标题(类似于说明)total=None, 预期的迭代次数leave=True,file=None,ncols=None, 可以自定义进度条的总长度mininterval=0.1, 最小的更新间隔maxinterval=10.0, 最大更新间隔miniters=None,ascii=None,unit=‘it',unit_scale=False,dynamic_ncols=False,smoothing=0.3,bar_format=None,initial=0,position=None,postfix 以字典形式传入 详细信息 例如 速度= 10,

示例
对于任意list的使用
alist = list('letters')bar = tqdm(alist)for letter in bar:bar.set_description(f"Now get {letter}")

输出结果如下:
python进度条显示-tqmd模块的实现示例
文章图片

传入任意list
pbar = tqdm(["a", "b", "c", "d"])for char in pbar:pbar.set_description("Processing %s" % char)

手动控制更新
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()

示例:
结合pandas的使用
import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randint(0, 100, (10000000, 6)))tqdm.pandas(desc="my bar!")df.progress_apply(lambda x: x**2)

输出结果如下:
python进度条显示-tqmd模块的实现示例
文章图片

示例
在Shell的tqdm用法
$ time find . -name '*.py' -exec cat \{} \; | wc -l857365 real0m3.458suser0m0.274ssys0m3.325s $ time find . -name '*.py' -exec cat \{} \; | tqdm | wc -l857366it [00:03, 246471.31it/s]857365 real0m3.585suser0m0.862ssys0m3.358s

使用的参数:
$ find . -name '*.py' -exec cat \{} \; |tqdm --unit loc --unit_scale --total 857366 >> /dev/null100%|███████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s]

备份一个目录:
$ 7z a -bd -r backup.7z docs/ | grep Compressing |tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s]

【python进度条显示-tqmd模块的实现示例】到此这篇关于python进度条显示-tqmd模块的实现示例的文章就介绍到这了,更多相关python -tqmd模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读