2013-04-08 87 views
-1

我需要在控制檯中創建簡單的進度。像:Python控制檯進度

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 

也就是說ehat我需要&但我認爲這可以被優化。有人可以幫忙嗎?

+0

可能重複的[控制檯中的文本進度條](http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console) – poke 2013-04-08 12:46:53

+0

不,我讀那個線程。它只有1線的進展。我需要10 – user2257564 2013-04-08 12:48:27

+0

任何人都可以幫助我嗎? – user2257564 2013-04-08 16:42:27

回答

0

我做如下

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

不要忘記,太多輸出到控制檯將您的循環減慢。

+0

語法突出顯示失敗。 //是整數除法運算符。 – 2013-04-08 12:43:42

+0

修正了對你:) – poke 2013-04-08 12:45:12

+0

不知道該怎麼做。所以這是特別的評論。 – 2013-04-08 12:50:15