(编辑:jimmy 日期: 2024/12/27 浏览:2)
import sys import time # 普通进度条 # 在代码迭代运行中进行统计计算,并使用格式化字符串输出代码运行进度 def progress_bar(): for i in range(1, 101): # 1-100 print("\r", end="") # \r 表示将光标的位置回退到本行的开头位置 print("Download progress: {} {}%".format("▋" * (i // 2), i), end="") sys.stdout.flush() # sys.stdout.flush()的作用就是显示地让缓冲区的内容输出。 time.sleep(0.02) progress_bar()
运行效果如下:
import time scale = 50 start = time.perf_counter() # 返回性能计数器的值(以秒为单位) for i in range(scale + 1): progress_bar = "▋" * i completion_ratio = (i / scale) * 100 dur_time = time.perf_counter() - start print("\rDownload Process:{:^3.0f}% [{}] {:.2f}s".format(completion_ratio, progress_bar, dur_time),end = "") time.sleep(0.1)
运行效果如下:
from time import sleep from tqdm import tqdm # 里面传入一个可迭代对象 for i in tqdm(range(1, 101)): # 模拟你的任务 sleep(0.05) sleep(0.5)
运行效果如下:
import time import progressbar def custom_len(value): # These characters take up more space characters = { '进': 3, '度': 3, } total = 0 for c in value: total += characters.get(c, 1) return total bar = progressbar.ProgressBar( widgets=[ '进度: ', progressbar.Bar(marker='#', left='|', right='|'), ' ', progressbar.Counter(format='%(value)02d/%(max_value)d'), ], len_func=custom_len, ) for i in bar(range(100)): time.sleep(0.05)
运行效果如下: