问题:Python打印状态栏和百分比
要实现如下状态栏:
[========== ] 45%
[================ ] 60%
[==========================] 100%
我希望将此打印到stdout,并保持刷新状态,而不是打印到另一行。这该怎么做?
To implement a status bar like below:
[========== ] 45%
[================ ] 60%
[==========================] 100%
I want to this to be printed out to stdout, and keep refreshing it, not print to another line. How to do this?
回答 0
您可以从中获得一个Python模块 PyPI将其称为progressbar
实现这样的功能。如果您不介意添加依赖项,那么这是一个很好的解决方案。否则,请选择其他答案之一。
一个简单的用法示例:
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 install progressbar
如果您更喜欢pip。
There’s a Python module that you can get from PyPI called progressbar
that implements such functionality. If you don’t mind adding a dependency, it’s a good solution. Otherwise, go with one of the other answers.
A simple example of how to use it:
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()
To install it, you can use easy_install progressbar
, or pip install progressbar
if you prefer pip.
回答 1
该'\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上使用。
The '\r'
character (carriage return) resets the cursor to the beginning of the line and allows you to write over what was previously on the line.
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)
I’m not 100% sure if this is completely portable across all systems, but it works on Linux and OSX at the least.
回答 2
我发现了有用的库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
可以包装任何可迭代的对象。
I found useful library tqdm (https://github.com/tqdm/tqdm/, previously: https://github.com/noamraph/tqdm). It automatically estimates time of completion and can be used as iterator.
Usage:
import tqdm
import time
for i in tqdm.tqdm(range(1000)):
time.sleep(0.01)
# or other long operations
Results in:
|####------| 450/1000 45% [elapsed: 00:04 left: 00:05, 99.15 iters/sec]
tqdm
can wrap any iterable.
回答 3
您可以使用\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()
You can use \r
(carriage return). Demo:
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()
回答 4
在这里,您可以使用以下代码作为函数:
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()
Here you can use following code as a function:
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()
With use of .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()
回答 5
根据上述答案以及有关CLI进度栏的其他类似问题,我认为我对所有这些答案都有一个通用的答案。在https://stackoverflow.com/a/15860757/2254146上进行检查
这是该函数的副本,但已进行修改以适合您的样式:
import time, sys
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
barLength = 20 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rPercent: [{0}] {1}% {2}".format( "="*block + " "*(barLength-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
好像
百分比:[====================] 99.0%
based on the above answers and other similar questions about CLI progress bar, I think I got a general common answer to all of them. Check it at https://stackoverflow.com/a/15860757/2254146
Here is a copy of the function, but modified to fit your style:
import time, sys
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
barLength = 20 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rPercent: [{0}] {1}% {2}".format( "="*block + " "*(barLength-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
Looks like
Percent: [====================] 99.0%
回答 6
如果您正在开发命令行界面,建议您看一下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%
If you are developing a command line interface, I suggest you to take a look at click
which is very nice:
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)
Here the output you get:
$ python test.py
[====================================] 100%
[====================================] 100%
[========= ] 27%
回答 7
使用以下功能进一步改进:
import sys
def printProgressBar(i,max,postText):
n_bar =10 #size of progress bar
j= i/max
sys.stdout.write('\r')
sys.stdout.write(f"[{'=' * int(n_bar * j):{n_bar}s}] {int(100 * j)}% {postText}")
sys.stdout.flush()
调用示例:
total=33
for i in range(total):
printProgressBar(i,total,"blah")
sleep(0.05)
输出:
[================================================ ] 96% blah
Further improving, using a function as :
import sys
def printProgressBar(i,max,postText):
n_bar =10 #size of progress bar
j= i/max
sys.stdout.write('\r')
sys.stdout.write(f"[{'=' * int(n_bar * j):{n_bar}s}] {int(100 * j)}% {postText}")
sys.stdout.flush()
calling example:
total=33
for i in range(total):
printProgressBar(i,total,"blah")
sleep(0.05)
output:
[================================================ ] 96% blah
回答 8
今天,在尝试了Mark Rushakoff的解决方案后,我遇到了这个问题
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)
我可以说这在使用Python 3.4.3 64位的W7-64上可以正常工作,但只能在本机控制台中使用。但是,当使用spyder 3.0.0dev的内置控制台时,仍然存在断行符。由于这花了我一些时间来弄清楚,因此我想在这里报告这一观察结果。
I came upon this thread today and after having tried out this solution from Mark Rushakoff
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)
I can say that this works fine on W7-64 with python 3.4.3 64-bit but only in the native console. However when using the built-in console of spyder 3.0.0dev, the line breaks are still/again present. As this took me some time to figure out, I’d like to report this observation here.
回答 9
在这里和其他地方的一些答案的基础上,我编写了这个简单的函数,它显示进度条和经过/估计的剩余时间。应该可以在大多数基于Unix的计算机上使用。
import time
import sys
percent = 50.0
start = time.time()
draw_progress_bar(percent, start)
def draw_progress_bar(percent, start, barLen=20):
sys.stdout.write("\r")
progress = ""
for i in range(barLen):
if i < int(barLen * percent):
progress += "="
else:
progress += " "
elapsedTime = time.time() - start;
estimatedRemaining = int(elapsedTime * (1.0/percent) - elapsedTime)
if (percent == 1.0):
sys.stdout.write("[ %s ] %.1f%% Elapsed: %im %02is ETA: Done!\n" %
(progress, percent * 100, int(elapsedTime)/60, int(elapsedTime)%60))
sys.stdout.flush()
return
else:
sys.stdout.write("[ %s ] %.1f%% Elapsed: %im %02is ETA: %im%02is " %
(progress, percent * 100, int(elapsedTime)/60, int(elapsedTime)%60,
estimatedRemaining/60, estimatedRemaining%60))
sys.stdout.flush()
return
Building on some of the answers here and elsewhere, I’ve written this simple function which displays a progress bar and elapsed/estimated remaining time. Should work on most unix-based machines.
import time
import sys
percent = 50.0
start = time.time()
draw_progress_bar(percent, start)
def draw_progress_bar(percent, start, barLen=20):
sys.stdout.write("\r")
progress = ""
for i in range(barLen):
if i < int(barLen * percent):
progress += "="
else:
progress += " "
elapsedTime = time.time() - start;
estimatedRemaining = int(elapsedTime * (1.0/percent) - elapsedTime)
if (percent == 1.0):
sys.stdout.write("[ %s ] %.1f%% Elapsed: %im %02is ETA: Done!\n" %
(progress, percent * 100, int(elapsedTime)/60, int(elapsedTime)%60))
sys.stdout.flush()
return
else:
sys.stdout.write("[ %s ] %.1f%% Elapsed: %im %02is ETA: %im%02is " %
(progress, percent * 100, int(elapsedTime)/60, int(elapsedTime)%60,
estimatedRemaining/60, estimatedRemaining%60))
sys.stdout.flush()
return
回答 10
这是一个非常简单的方法,可用于任何循环。
#!/usr/bin/python
for i in range(100001):
s = ((i/5000)*'#')+str(i)+(' %')
print ('\r'+s),
This is quite a simple approach can be used with any loop.
#!/usr/bin/python
for i in range(100001):
s = ((i/5000)*'#')+str(i)+(' %')
print ('\r'+s),
回答 11
最简单的还是
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()
关键是将整数类型转换为字符串。
Easiest is still
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()
Key is to convert the integer type to string.
回答 12
如Mark Rushakoff的解决方案所述,您可以输出回车符sys.stdout.write('\r')
,以将光标重置到行首。要推广该解决方案,同时还实现Python 3的f-Strings,您可以使用
from time import sleep
import sys
n_bar = 50
iterable = range(33) # for demo purposes
n_iter = len(iterable)
for i, item in enumerate(iterable):
j = (i + 1) / n_iter
sys.stdout.write('\r')
sys.stdout.write(f"[{'=' * int(n_bar * j):{n_bar}s}] {int(100 * j)}%")
sys.stdout.flush()
sleep(0.05)
# do something with <item> here
As described in Mark Rushakoff’s solution, you can output the carriage return character, sys.stdout.write('\r')
, to reset the cursor to the beginning of the line. To generalize that solution, while also implementing Python 3’s f-Strings, you could use
from time import sleep
import sys
n_bar = 50
iterable = range(33) # for demo purposes
n_iter = len(iterable)
for i, item in enumerate(iterable):
j = (i + 1) / n_iter
sys.stdout.write('\r')
sys.stdout.write(f"[{'=' * int(n_bar * j):{n_bar}s}] {int(100 * j)}%")
sys.stdout.flush()
sleep(0.05)
# do something with <item> here
回答 13
尝试PyProg。PyProg是Python的开放源代码库,用于创建超级可自定义的进度指示器和栏。
当前版本为1.0.2;它托管在Github上,可在PyPI上使用(下面的链接)。它与Python 3&2兼容,也可以与Qt Console一起使用。
真的很容易使用。如下代码:
import pyprog
from time import sleep
# Create Object
prog = pyprog.ProgressBar(" ", " ", total=34, bar_length=26, complete_symbol="=", not_complete_symbol=" ", wrap_bar_prefix=" [", wrap_bar_suffix="] ", progress_explain="", progress_loc=pyprog.ProgressBar.PROGRESS_LOC_END)
# Update Progress Bar
prog.update()
for i in range(34):
# Do something
sleep(0.1)
# Set current status
prog.set_stat(i + 1)
# Update Progress Bar again
prog.update()
# Make the Progress Bar final
prog.end()
会产生您想要的东西(甚至是棒的长度!):
[=========== ] 45%
[=============== ] 60%
[==========================] 100%
有关自定义进度栏的更多选项,请访问此网站的Github页面。
我之所以制作PyProg,是因为我需要一个简单但可自定义的进度条库。您可以使用轻松安装它pip install pyprog
。
PyProg Github:https : //github.com/Bill13579/pyprog
PyPI:https ://pypi.python.org/pypi/pyprog/
Try PyProg. PyProg is an open-source library for Python to create super customizable progress indicators & bars.
It is currently at version 1.0.2; it is hosted on Github and available on PyPI (Links down below). It is compatible with Python 3 & 2 and it can also be used with Qt Console.
It is really easy to use. The following code:
import pyprog
from time import sleep
# Create Object
prog = pyprog.ProgressBar(" ", " ", total=34, bar_length=26, complete_symbol="=", not_complete_symbol=" ", wrap_bar_prefix=" [", wrap_bar_suffix="] ", progress_explain="", progress_loc=pyprog.ProgressBar.PROGRESS_LOC_END)
# Update Progress Bar
prog.update()
for i in range(34):
# Do something
sleep(0.1)
# Set current status
prog.set_stat(i + 1)
# Update Progress Bar again
prog.update()
# Make the Progress Bar final
prog.end()
will produce exactly what you want (even the bar length!):
[=========== ] 45%
[=============== ] 60%
[==========================] 100%
For more options to customize the progress bar, go to the Github page of this website.
I actually made PyProg because I needed a simple but super customizable progress bar library. You can easily install it with: pip install pyprog
.
PyProg Github: https://github.com/Bill13579/pyprog
PyPI: https://pypi.python.org/pypi/pyprog/
回答 14
使用@ Mark-Rushakoff答案,我制定了一种更简单的方法,无需调用sys库。它适用于Python3。在Windows中测试:
from time import sleep
for i in range(21):
# the exact output you're looking for:
print ("\r[%-20s] %d%%" % ('='*i, 5*i), end='')
sleep(0.25)
Using @Mark-Rushakoff answer, I worked out a simpler approach, no need to call the sys library. It works with Python 3. Tested in Windows:
from time import sleep
for i in range(21):
# the exact output you're looking for:
print ("\r[%-20s] %d%%" % ('='*i, 5*i), end='')
sleep(0.25)
回答 15
这是我使用@ Mark-Rushakoff解决方案所做的事情。自适应地调整端子宽度。
from time import sleep
import os
import sys
from math import ceil
l = list(map(int,os.popen('stty size','r').read().split()))
col = l[1]
col = col - 6
for i in range(col):
sys.stdout.write('\r')
getStr = "[%s " % ('='*i)
sys.stdout.write(getStr.ljust(col)+"]"+"%d%%" % (ceil((100/col)*i)))
sys.stdout.flush()
sleep(0.25)
print("")
Here is something I have made using the solution by @Mark-Rushakoff. To adaptively adjust to the terminal width.
from time import sleep
import os
import sys
from math import ceil
l = list(map(int,os.popen('stty size','r').read().split()))
col = l[1]
col = col - 6
for i in range(col):
sys.stdout.write('\r')
getStr = "[%s " % ('='*i)
sys.stdout.write(getStr.ljust(col)+"]"+"%d%%" % (ceil((100/col)*i)))
sys.stdout.flush()
sleep(0.25)
print("")