终端中的文字进度条,带有方块字[关闭]。

您所在的位置:网站首页 python里的round中的n怎么用 终端中的文字进度条,带有方块字[关闭]。

终端中的文字进度条,带有方块字[关闭]。

2023-03-21 08:56| 来源: 网络整理| 查看: 265

Python 3 A Simple, Customizable Progress Bar

下面是我经常使用的许多答案的汇总(不需要进口)。

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() Sample Usage 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) Sample Output Progress: |█████████████████████████████████████████████-----| 90.0% Complete Update

有人在评论中讨论了一个选项,即允许进度条根据终端窗口的宽度动态调整。虽然我不推荐这样做,但这里有一个gist实现了这一功能(并指出了注意事项)。

Single-Call Version of The Above

下面的一个评论提到了一个不错的答案在回答一个类似的问题时发布的。我喜欢它所展示的易用性,并写了一个类似的,但选择了不导入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() Sample Usage 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) Sample Output 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)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3