Python 3
一个简单的、可定制的进度条
以下是我经常使用的以下许多答案的汇总(无需导入)。
Note:本答案中的所有代码都是为 Python 3 创建的;请参阅答案末尾以将此代码与 Python 2 一起使用。
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
使用示例
import time
# A List of Items
items = list(range(0, 57))
l = len(items)
# Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for i, item in enumerate(items):
# Do stuff...
time.sleep(0.1)
# Update Progress Bar
printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
样本输出
Progress: |█████████████████████████████████████████████-----| 90.0% Complete
Update
评论中讨论了允许进度条动态调整到终端窗口宽度的选项。虽然我不推荐这样做,但这里有一个gist实现此功能(并注明注意事项)。
上述的单次调用版本
下面的评论引用了一个很好的answer发布于回答类似的问题。我喜欢它所展示的易用性,并写了一个类似的,但选择忽略了sys
模块,同时添加了原有的一些功能printProgressBar
上面的函数。
与上面的原始函数相比,这种方法的一些好处包括消除了对函数的初始调用以在 0% 处打印进度条以及使用enumerate
成为可选的(即不再明确需要使该功能起作用)。
def progressBar(iterable, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iterable - Required : iterable object (Iterable)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
total = len(iterable)
# Progress Bar Printing Function
def printProgressBar (iteration):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Initial Call
printProgressBar(0)
# Update Progress Bar
for i, item in enumerate(iterable):
yield item
printProgressBar(i + 1)
# Print New Line on Complete
print()
使用示例
import time
# A List of Items
items = list(range(0, 57))
# A Nicer, Single-Call Usage
for item in progressBar(items, prefix = 'Progress:', suffix = 'Complete', length = 50):
# Do stuff...
time.sleep(0.1)
样本输出
Progress: |█████████████████████████████████████████████-----| 90.0% Complete
Python 2
要在 Python 2 中使用上述函数,请在脚本顶部将编码设置为 UTF-8:
# -*- coding: utf-8 -*-
并替换此行中的 Python 3 字符串格式:
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
使用 Python 2 字符串格式:
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = printEnd)