python:在同一行打印并覆盖前一行

时间:2016-11-21 17:16:49

标签: python

我编写以下代码来生成文本进度条:

for ii in range(len(imgPaths)):
    temp = misc.imread(imgPaths[ii])
    imgData[ii,:,:,:] = temp
    print 100*ii/len(imgPaths), "Percent complete\r",

代码生成如下进度:

99 Percent complete Percent complete5 Percent complete7 Percent complete10 Percent complete12 Percent complete15 Percent complete17 Percent complete20 Percent complete22 Percent complete24 Percent complete27 Percent complete29 Percent complete32 Percent complete34 Percent complete37 Percent complete40 Percent complete42 Percent complete45 Percent complete48 Percent complete50 Percent complete53 Percent complete56 Percent complete58 Percent complete60 Percent complete62 Percent complete65 Percent complete67 Percent complete69 Percent complete71 Percent complete73 Percent complete76 Percent complete78 Percent complete81 Percent complete82 Percent complete84 Percent complete87 Percent complete90 Percent complete91 Percent complete94 Percent complete96 Percent complete99 Percent complete

似乎打印相同但过于冗余。 有没有办法以有效的方式简化打印结果。 我希望更新的进度覆盖前一个进度。

1 个答案:

答案 0 :(得分:1)

请改为尝试:

print "\r{} Percent complete".format(100*ii/len(imgPaths)),