实现如下所示的状态栏:

[==========                ]  45%
[================          ]  60%
[==========================] 100%

我想把这个打印到标准输出,并保持刷新,而不是打印到另一行。如何做到这一点?


当前回答

你可以使用\r(回车)。演示:

import sys
total = 10000000
point = total / 100
increment = total / 20
for i in xrange(total):
    if(i % (5 * point) == 0):
        sys.stdout.write("\r[" + "=" * (i / increment) +  " " * ((total - i)/ increment) + "]" +  str(i / point) + "%")
        sys.stdout.flush()

其他回答

'\r'字符(回车)将光标重置到行首,并允许您重写该行之前的内容。

from time import sleep
import sys

for i in range(21):
    sys.stdout.write('\r')
    # the exact output you're looking for:
    sys.stdout.write("[%-20s] %d%%" % ('='*i, 5*i))
    sys.stdout.flush()
    sleep(0.25)

我不能100%确定这是否可以在所有系统上完全移植,但它至少可以在Linux和OSX上运行。

在这里你可以使用以下代码作为函数:

def drawProgressBar(percent, barLen = 20):
    sys.stdout.write("\r")
    progress = ""
    for i in range(barLen):
        if i < int(barLen * percent):
            progress += "="
        else:
            progress += " "
    sys.stdout.write("[ %s ] %.2f%%" % (progress, percent * 100))
    sys.stdout.flush()

使用.format:

def drawProgressBar(percent, barLen = 20):
    # percent float from 0 to 1. 
    sys.stdout.write("\r")
    sys.stdout.write("[{:<{}}] {:.0f}%".format("=" * int(barLen * percent), barLen, percent * 100))
    sys.stdout.flush()

这是一个简单的0导入进度条形码

#!/usr/bin/python3
def progressbar(current_value,total_value,bar_lengh,progress_char): 
    percentage = int((current_value/total_value)*100)                                                # Percent Completed Calculation 
    progress = int((bar_lengh * current_value ) / total_value)                                       # Progress Done Calculation 
    loadbar = "Progress: [{:{len}}]{}%".format(progress*progress_char,percentage,len = bar_lengh)    # Progress Bar String
    print(loadbar, end='\r')                                                                         # Progress Bar Output

if __name__ == "__main__":
    the_list = range(1,301) 
    for i in the_list:
        progressbar(i,len(the_list),30,'■')
    print("\n")

进度: [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

import progressbar
import time

# Function to create  
def animated_marker():
    widgets = ['Loading: ', progressbar.Bar('=', '[', ']', '-'), progressbar.Percentage()]
    bar = progressbar.ProgressBar(max_value=200,widgets=widgets).start() 
      
    for i in range(200): 
        time.sleep(0.1)
        bar.update(i+1)
    bar.finish()

# Driver's code 
animated_marker()

没有一个答案能完全满足我的需求。所以我写了我自己的,如上所示。我需要的功能:

只传递步数和总步数,它就完成了计算完成百分比的困难工作。 使用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个刻度。 移除整块之间的间隙。 添加颜色支持。