Python打印出状态栏和百分比 [英] Python to print out status bar and percentage

查看:58
本文介绍了Python打印出状态栏和百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现如下状态栏:

[==========                ]  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?

推荐答案

有一个 Python 模块,您可以从 PyPI 获得 调用了实现此类功能的 progressbar.如果您不介意添加依赖项,这是一个很好的解决方案.否则,请选择其他答案之一.

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()

要安装它,您可以使用 easy_install progressbar,或者如果您喜欢 pip,可以使用 pip install progressbar.

To install it, you can use easy_install progressbar, or pip install progressbar if you prefer pip.

这篇关于Python打印出状态栏和百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆