【问题标题】:Python console progressPython 控制台进度
【发布时间】:2013-04-08 12:38:45
【问题描述】:

我需要在控制台中创建简单的进度。喜欢:

Progress: [######] 30% out of 432 combinations
Last 10 tried:
12.12.2013 - this1
12.12.2013 - this2
...
12.12.2013 - this10

很简单。我发现了一个丑陋的方法:每次都执行 clr() 并打印(用于进度)。 但我不明白,我怎样才能用不同的数据打印 10 个字符串。 10次​​打印很丑,数据一直在变化。有没有办法很好地登录控制台?对不起我的英语。

我的示例代码:

import os
import time


def cls():
    os.system(['clear', 'cls'][os.name == 'nt'])


def progress(i, l=None):
    total = 400
    a = 100-i*100/total
    cls()
    print "Progress: [%-33s] %d%% out of %d combinations" % ("#"*(a/3), a, total)
    print "Last 10 tried:"
    for j in l:
        print j
    time.sleep(0.3)


i = 400
l = []
while i > 0:
    l.append("%s - %s" % ('11.21.2013', i))
    if len(l) >= 10:
        progress(i, l)
        l = []
    i -= 1

这就是我需要的&但我认为它可以优化。有人可以帮忙吗?

【问题讨论】:

标签: python progress


【解决方案1】:

我是这样做的

print (current_line * 100 // total_lines, "%", end = '\r')

不要忘记,控制台输出过多会减慢您的循环速度。

【讨论】:

  • 语法高亮失败。 // 是整数除法运算符。
  • 不知道该怎么做。所以是特别的评论。
猜你喜欢
  • 2015-11-18
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 2013-05-14
  • 2016-09-24
  • 2021-11-14
  • 2010-11-28
  • 1970-01-01
相关资源
最近更新 更多