实现如下所示的状态栏:
[========== ] 45%
[================ ] 60%
[==========================] 100%
我想把这个打印到标准输出,并保持刷新,而不是打印到另一行。如何做到这一点?
实现如下所示的状态栏:
[========== ] 45%
[================ ] 60%
[==========================] 100%
我想把这个打印到标准输出,并保持刷新,而不是打印到另一行。如何做到这一点?
当前回答
如果你正在开发一个命令行界面,我建议你看看click,它非常好:
import click
import time
for filename in range(3):
with click.progressbar(range(100), fill_char='=', empty_char=' ') as bar:
for user in bar:
time.sleep(0.01)
下面是你得到的输出:
$ python test.py
[====================================] 100%
[====================================] 100%
[========= ] 27%
其他回答
没有一个答案能完全满足我的需求。所以我写了我自己的,如上所示。我需要的功能:
只传递步数和总步数,它就完成了计算完成百分比的困难工作。 使用60个字符,将它们分成480个“刻度”,每个刻度产生0.21%的收益。如果没有勾号,每个字符只占1.67%。 支持将标题放在前面。 行尾完成百分比可选。 可变长度的进度条,默认为60个字符或480个“刻度”。 设置进度条颜色,默认为绿色。
如何调用进度显示
调用进度显示非常简单。对于上面的样例.gif函数调用使用:
percent_complete(step, total_steps, title="Convert Markdown")
在CSV格式的Stack Exchange Data Dump中,len(rows)的total_steps约为2,500。该步骤是当前行号,因为每个Stack Exchange Markdown问答都转换为Kramdown(用于GitHub页面)。
Python代码
代码很简单,但比其他答案长一些:
def percent_complete(step, total_steps, bar_width=60, title="", print_perc=True):
import sys
# UTF-8 left blocks: 1, 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8
utf_8s = ["█", "▏", "▎", "▍", "▌", "▋", "▊", "█"]
perc = 100 * float(step) / float(total_steps)
max_ticks = bar_width * 8
num_ticks = int(round(perc / 100 * max_ticks))
full_ticks = num_ticks / 8 # Number of full blocks
part_ticks = num_ticks % 8 # Size of partial block (array index)
disp = bar = "" # Blank out variables
bar += utf_8s[0] * int(full_ticks) # Add full blocks into Progress Bar
# If part_ticks is zero, then no partial block, else append part char
if part_ticks > 0:
bar += utf_8s[part_ticks]
# Pad Progress Bar with fill character
bar += "▒" * int((max_ticks/8 - float(num_ticks)/8.0))
if len(title) > 0:
disp = title + ": " # Optional title to progress display
# Print progress bar in green: https://stackoverflow.com/a/21786287/6929343
disp += "\x1b[0;32m" # Color Green
disp += bar # Progress bar to progress display
disp += "\x1b[0m" # Color Reset
if print_perc:
# If requested, append percentage complete to progress display
if perc > 100.0:
perc = 100.0 # Fix "100.04 %" rounding error
disp += " {:6.2f}".format(perc) + " %"
# Output to terminal repetitively over the same line using '\r'.
sys.stdout.write("\r" + disp)
sys.stdout.flush()
Python代码注释
以下几点:
The [ .... ] bracket placeholders requirement in the question are not necessary because there is the fill characters that serve the same purpose. This saves two extra characters to make the progress bar wider. The bar_width keyword parameter can be used depending on screen width. The default of 60 seems a good fit for most purposes. The print_perc=True keyword parameter default can be overridden by passing print_perc=False when calling the function. This would allow a longer progress bar. The title="" keyword parameter defaults to no title. Should you desire one use title="My Title" and : will automatically be added to it. When your program finishes remember to call sys.stdout.write("\r") followed by sys.stdout.flush() to clear the progress display line.
总结
这个答案比其他答案长一点,但重要的是要注意它是一个完整的解决方案,而不是需要添加更多代码的解决方案的一部分。
另一点是这个解决方案没有依赖关系,也不需要安装任何额外的东西。Python支持UTF-8字符集,不需要额外设置gnome-terminal。如果你使用的是python2.7,在shebang后面可能需要# -*- coding: utf-8 -*-。IE作为程序的第二行。
该函数可以转换为具有单独的init、update、pause(用于将调试内容打印到屏幕上)、resume和close方法的类。
这个函数是从bash脚本转换过来的:
如何将进度条添加到shell脚本?
bash脚本将显示索尼电视音量与libnotify-bin(弹出泡泡消息)每当电视音量改变。如果您对bash进度条感兴趣,请访问Stack Overflow链接。
编辑于2022年1月30日
每个角色从4个刻度改为8个刻度。 移除整块之间的间隙。 添加颜色支持。
你可以从PyPI获得一个名为progressbar的Python模块,它实现了这样的功能。如果您不介意添加依赖项,这是一个很好的解决方案。否则,就用其他答案吧。
一个简单的例子,如何使用它:
import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=20, \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for i in xrange(20):
bar.update(i+1)
sleep(0.1)
bar.finish()
要安装它,可以使用easy_install progressbar,如果您更喜欢pip,则可以使用pip install progressbar。
如果你正在开发一个命令行界面,我建议你看看click,它非常好:
import click
import time
for filename in range(3):
with click.progressbar(range(100), fill_char='=', empty_char=' ') as bar:
for user in bar:
time.sleep(0.01)
下面是你得到的输出:
$ python test.py
[====================================] 100%
[====================================] 100%
[========= ] 27%
我发现有用的库tqdm (https://github.com/tqdm/tqdm/,以前是:https://github.com/noamraph/tqdm)。它可以自动估计完成时间,也可以用作迭代器。
用法:
import tqdm
import time
for i in tqdm.tqdm(range(1000)):
time.sleep(0.01)
# or other long operations
结果:
|####------| 450/1000 45% [elapsed: 00:04 left: 00:05, 99.15 iters/sec]
TQDM可以包装任何可迭代对象。
最简单的还是
import sys
total_records = 1000
for i in range (total_records):
sys.stdout.write('\rUpdated record: ' + str(i) + ' of ' + str(total_records))
sys.stdout.flush()
关键是将整数类型转换为字符串。