关于进度条:CLI中的Python ProgressBar

Python ProgressBar in CLI

我正在使用Python 3.x,并下载了progressbar2库;但是,我正在努力在程序中使用它。当在指定范围内时,一切正常,但是如何使用自己的代码实时更新进度栏?我希望进度栏在文本列表的每次迭代中都将跳到20%,并且会首先显示,并在打印出结果时刷新进度。

我还发现了另一个与Stack上的我类似的问题,但是我更喜欢使用progressbar2库,因为它已经存在。考虑以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import time
import progressbar

texts = ['a', 'b', 'c', 'd', 'e']

def basic_widget_example (texts):

    widgets = [progressbar.Percentage (), progressbar.Bar ()]
    bar = progressbar.ProgressBar (widgets = widgets, max_value = len (texts)).start ()
    for i in range (len (texts)):
        print (texts [i])
        time.sleep (0.1)
        bar.update (i + 1)
    bar.finish ()

结果:

1
2
3
4
5
6
N/A%|                                                                                                               |a
 20%|######################                                                                                         |b
 40%|############################################                                                                   |c
 60%|##################################################################                                             |d
 80%|########################################################################################                       |e
 100%|###############################################################################################################|

我希望它看起来像这样:

1
2
3
4
5
6
100%|#############################################################|
a
b                                                                                                                    
c                                                                                                                    
d                                                                                                                    
e

一种解决方案是创建2个for循环,但是如您所见,进度条显示不再准确或与我的任务保持同步。即使我的任务可能需要2分钟才能完成,进度条也将在不到一秒钟的时间内显示100%。知道如何在尽可能接近准确的情况下实现这一目标吗?

1
2
3
4
5
6
7
8
9
10
11
12
def basic_widget_example (texts):

    widgets = [progressbar.Percentage (), progressbar.Bar ()]
    bar = progressbar.ProgressBar (widgets = widgets, max_value = len (texts)).start ()
    for i in range (len (texts)):
        # Do something
        time.sleep (0.1)
        bar.update (i + 1)
    bar.finish ()

    for text in texts:
        print (text)


我误解了进度条的用法。进度条的目的是让用户知道在不需要与用户交流的地方进行计算。在幕后进行一些字符串操作或从一组数据创建列表的情况下,这很有用。一旦获得结果并准备好将其输出到屏幕上,此时就不需要进度条了。

例如,我创建了一个1到50的整数列表,将其添加1(后台处理),并将其附加到另一个将用于模拟输出的列表中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
lst = [i for i in range (50)]
basic_widget_example (lst)

def basic_widget_example (lst):

    newlist = []

    widgets = [progressbar.Percentage (), progressbar.Bar ()]
    bar = progressbar.ProgressBar (widgets = widgets, max_value = len (lst)).start ()
    for i in range (len (lst)):
        # Do something
        newlist = [x + 1 for x in lst]
        time.sleep (0.1)
        bar.update (i + 1)
    bar.finish ()

    print (newlist [:5])

结果:

1
2
100%|###############################################################################################################|
[1, 2, 3, 4, 5]

进度条将以列表的长度递增,该列表的长度是" max_value"指定的50。每次迭代将产生两个"#"。

还值得一提的是time.sleep(值)对应于进度条完成所花费的时间。在我的示例中,50次迭代* 0.1秒= 5秒。这意味着进度条将在5秒内100%完成。这不是实时的。如果需要实时计算睡眠值,则需要进行进一步的计算。一种想法是计算每次迭代花费的时间或取平均值,并将其用作time.sleep的值。