1

我有一个脚本,它使用一个简单的 while 循环来显示一个进度条,但它似乎没有像我预期的那样工作:

count = 1
maxrecords = len(international)
p = ProgressBar("Blue")
t = time
while count < maxrecords:
    print 'Processing %d of %d' % (count, maxrecords)
    percent = float(count) / float(maxrecords) * 100
    p.render(int(percent))
    t.sleep(0.5)
    count += 1

它似乎在“p.render ...”处循环,并且不会返回“print 'Processing %d of %d ...'”。

更新:我很抱歉。似乎 ProgressBar.render() 在渲染进度条时删除了“print 'Processing...”的输出。进度条来自http://nadiana.com/animated-terminal-progress-bar-in-python

4

4 回答 4

5

我看到您在我的网站上使用 ProgressBar 实现。如果要打印消息,可以在 render 中使用 message 参数

p.render(percent, message='Processing %d of %d' % (count, maxrecords))
于 2009-08-21T15:28:32.657 回答
3

这不是在 Python 中编写循环的方式。

maxrecords = len(international)
p = ProgressBar("Blue")
for count in range(1, maxrecords):
    print 'Processing %d of %d' % (count, maxrecords)
    percent = float(count) / float(maxrecords) * 100
    p.render(int(percent))
    time.sleep(0.5)

如果你真的想对记录做点什么,而不仅仅是渲染栏,你可以这样做:

maxrecords = len(international)
for count, record in enumerate(international):
    print 'Processing %d of %d' % (count, maxrecords)
    percent = float(count) / float(maxrecords) * 100
    p.render(int(percent))
    process_record(record)   # or whatever the function is
于 2009-08-21T15:01:58.457 回答
2

实施是为了ProgressBar.render()什么?我假设它正在输出移动光标的终端控制字符,以便覆盖以前的输出。这可能会造成控制流未按应有的方式工作的错误印象。

于 2009-08-21T15:08:47.803 回答
1

(1) [不是问题的一部分,但是...]t = time之后的很晚,t.sleep(0.5)对于任何看到裸露t并不得不向后阅读才能找到它的人来说,这将是一个烦恼的根源。

(2) [不是问题的一部分,但是 ...]count永远不能以与 . 相同的值进入循环maxrecords。例如,如果maxrecords为 10,则循环中的代码只执行 9 次。

(3) 您展示的代码中没有任何内容可以支持它“在 p.render() 处循环”的想法——除非渲染方法本身在其 arg 为零时循环,如果maxrecords是17909.尝试将 p.render(....) 暂时替换为 (say)

print "pretend-render: pct =", int(percent)

于 2009-08-21T15:28:48.740 回答