看到一个推文:Python制作进度条,原来有这么多方法!
另一位大佬的帖子:Python 实现进度条的六种方式
感觉很有意思!分享给各位!
- 原文:https://towardsdatascience.com/learning-to-use-progress-bars-in-python-2dc436de81e5
1. 普通进度条
实例:
import sys
import time
# 定义函数
def progress_bar():
for i in range(1, 101):
print("\r", end="")
print("Download progress: {}%: ".format(i), " ▋" * (i // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
if __name__ == '__main__':
progress_bar()
结果:
2. progress
- 官网:https://pypi.org/project/progress/1.5/
安装:
pip install progress==1.5
具体详情可前往官网查看详情!
实例:
# progress
from progress.bar import IncrementalBar
import time
mylist = [1,2,3,4,5,6,7,8]
bar = IncrementalBar( 'Countdown' , max = len(mylist))
for item in mylist:
bar.next()
time.sleep(1)
bar.finish()
结果:
3. tqdm
- 官网:https://pypi.org/project/tqdm/
- doc: https://tqdm.github.io/
# 安装
pip install tqdm
实例:
# tqdm
from tqdm import tqdm
import time
text = ""
for char in tqdm(["a", "b", "c", "d","e"]):
time.sleep(0.25)
text = text + char
print(text)
结果:
4. alive-progress
- 官网: https://github.com/rsalmei/alive-progress
pip install alive-progress
实例:
from alive_progress import alive_bar
import time # Python 内置
mylist = [1,2,3,4,5,6,7,8]
with alive_bar(len(mylist)) as bar:
for i in mylist:
bar()
time.sleep(0.1) # 时间间隔
结果: