2009-08-21 76 views
1

我有一個使用一個簡單的while循環顯示進度條,但似乎並不奏效,因爲我預料的腳本:幫助與Python 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 ...」並且不返回到「打印」處理%d ...的%d ...'「。

更新:我的歉意。看起來ProgressBar.render()在呈現進度條時刪除了「print'Processing ...」的輸出。進度條是從http://nadiana.com/animated-terminal-progress-bar-in-python

+0

你是說計數值沒有發生變化? – 2009-08-21 15:06:32

回答

2

什麼是ProgressBar.render()執行?我假設它正在輸出移動光標的終端控制字符,以使之前的輸出被覆蓋。這可能會造成控制流程無法正常工作的錯誤印象。

+0

這裏的輸出中:17909的0 % 0% 0% 0% 0%處理1 0% 0% – Francis 2009-08-21 15:14:08

+1

ProgressBar來自http://nadiana.com/animated-terminal-progress-bar-in-python – Francis 2009-08-21 15:15:00

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 
+1

用戶不滿意「Processing of 100」:-) – 2009-08-21 15:10:46

+0

感謝您的快速響應。我嘗試了你的第一個建議,它仍然不回到「打印」處理...「行。它停留在「p.render ...」行。 – Francis 2009-08-21 15:11:39

+1

它不是「卡住」@Francis。請在你的問題中提供其餘的代碼。 – 2009-08-21 15:17:26

5

我看到你在我的網站上使用ProgressBar實現。如果你想打印,您可以使用消息參數的消息在渲染

p.render(percent, message='Processing %d of %d' % (count, maxrecords)) 
+1

感謝ProgressBar;) – Francis 2009-08-21 15:32:05

+1

如果您發現任何錯誤,請讓我知道。我會很樂意解決它。 – 2009-08-21 15:33:32

+0

完美的作品。謝謝你的提示! – Francis 2009-08-21 15:39:48

1

(1)不是問題的一部分,但...] t = time尾隨其後由t.sleep(0.5)多是煩惱的根源任何人看到光禿禿的t並且不得不向後讀取以找到它是什麼。

(2)[不是問題的一部分,但是...] count永遠不能以與maxrecords相同的值進入循環。例如。如果maxrecords爲10,則循環中的代碼只執行9次。 (3)你所展示的代碼中沒有任何東西可以支持它「在p.render()循環」的想法 - 除非渲染方法本身在arg爲零時循環,這將會是如果maxrecords的情況下是17909.嘗試更換p.render(....)暫時與(比如說)

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

+0

感謝您的反饋。我明白你的觀點:#2。的確,循環直到'maxrecords'纔會處理 - 它總是小於1 – Francis 2009-08-21 15:49:50