【Python】進度條的實現
阿新 • • 發佈:2019-01-14
方式一
#!/usr/bin/env python
from __future__ import division
import sys,time
j = '#'
for i in range(1,61):
j += '#'
sys.stdout.write(str(int((i/60)*100))+'% '+j+'->'+ "\r")
sys.stdout.flush()
time.sleep(0.1)
print("end")
結果:
98
%
############################################################->
方式二
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'l' import sys from time import sleep def viewBar(i): """ 進度條效果 :param i: :return: """ output = sys.stdout for count in range(0, i + 1): second = 0.1 sleep(second) output.write('\rcomplete percent ----->:%.0f%%' % count) output.flush() viewBar(100)
結果:
方式三
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1, 500)):
sleep(0.01)
結果:
方式四
class ProgressBar(): def __init__(self, width=50): self.pointer = 0 self.width = width def __call__(self, x): # x in percent self.pointer = int(self.width * (x / 100.0)) return "|" + "#" * self.pointer + "-" * (self.width - self.pointer) + "|\n %d percent done" % int(x) import time, os pb = ProgressBar() for i in range(101): os.system('clear') print(pb(i)) time.sleep(0.1)
結果:
方式五
import sys
import time
def view_bar(num,total):
rate = num / total
rate_num = int(rate * 100)
#r = '\r %d%%' %(rate_num)
r = '\r%s>%d%%' % ('=' * rate_num, rate_num,)
sys.stdout.write(r)
sys.stdout.flush()
if __name__ == '__main__':
for i in range(0, 101):
time.sleep(0.1)
view_bar(i, 100)
結果: