1.引言

在Python项目中,有时需要输出程序的运行进度,会print出【1/100】【50/100】等简单的字符串,殊不知Python有一些超级好用的第三方库,几行代码就可以实现进度条显示,本文介绍两个常用的进度条库:tqdmprogressbar

2.tqdm

安装

pip install tqdm

案例:

import time
from tqdm import tqdm

for i in tqdm(range(100)):
    time.sleep(0.1)

python 命令行 进度条 python进度条程序_python

tqdm参数设置

  • desc:进度条标题
  • total:迭代总次数
  • ncols:进度条总长度
  • ascii:使用ASCII字符串作为进度条主体
  • bar_format:自定义字符串格式化输出
  • mininterval:最小更新间隔,单位:秒
  • maxinterval:最大更新间隔,单位:秒
  • postfix:以字典形式传入

tqdm自定义参数的代码实现

import time
from tqdm import tqdm

for i in tqdm(range(100), desc='Progress', ncols=100, ascii=' =', bar_format='{l_bar}{bar}|'):
    time.sleep(0.05)

python 命令行 进度条 python进度条程序_ci_02

3.progressbar

安装

pip install progressbar

案例:

import time
from progressbar import *

progress = ProgressBar()
for i in progress(range(100)):
    time.sleep(0.05)
    print('')

python 命令行 进度条 python进度条程序_字符串_03