Python to print out status bar and percentage

后端 未结 20 1035
野的像风
野的像风 2020-11-28 01:01

To implement a status bar like below:

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

相关标签:
20条回答
  • 2020-11-28 01:24

    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/

    0 讨论(0)
  • 2020-11-28 01:24

    Per Steven C. Howell's comment on Mark Rushakoff's answer

    j = (i + 1) / n
    stdout.write('\r')
    stdout.write('[%-20s] %d%%' % ('='*int(20*j), 100*j))
    stdout.flush()
    

    where i is the current item and n is the total number of items

    0 讨论(0)
提交回复
热议问题