Python实现进度条总结
先说一下文本系统的控制符:
\r: 将光标移动到当前行的首位而不换行;
\n: 将光标移动到下一行,并不移动到首位;
\r\n: 将光标移动到下一行首位。
环境:
root@ubuntu16:/alex/py/jingdutiao# python3
Python 3.5.2 (default, Jul 5 2016, 12:43:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
方式1:
root@ubuntu16:/alex/py/jingdutiao# cat test1.py
#!/usr/bin/env python
from __future__ import division
import sys,time
j = '#'
if __name__ == '__main__':
for i in range(1,61):
j += '#'
sys.stdout.write(str(int((i/60)*100))+'% '+j+'->'+ "\r")
sys.stdout.flush()
time.sleep(0.5)
print
root@ubuntu16:/alex/py/jingdutiao# python3 test1.py
98% ############################################################->
方式2:
root@ubuntu16:/alex/py/jingdutiao# cat test4.py
#!/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)
root@ubuntu16:/alex/py/jingdutiao# python3 test4.py
complete percent ----->:100%
方式3:
tqdm模块
tqdm是一个快速、扩展性强的进度条工具库,
其githup地址:https://github.com/tqdm/tqdm
1)安装:
直接使用pip安装:
pip install tqdm
2)使用:
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1, 500)):
sleep(0.01)
自己实操:在ubuntu上默认安装到2.7环境变量里去了
root@ubuntu16:/alex/py/jingdutiao# pip install tqdm
Collecting tqdm
Downloading tqdm-4.8.4-py2.py3-none-any.whl
Installing collected packages: tqdm
Successfully installed tqdm-4.8.4
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
pip install --upgrade pip
pip install tqdm
pip -V
cd /usr/local/lib/python2.7/dist-packages/
cp -r tqdm tqdm-4.8.4.dist-info/ /usr/local/lib/python3.5/dist-packages
root@ubuntu16:/alex/py/jingdutiao# cat test5.py
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1, 500)):
sleep(0.01)
root@ubuntu16:/alex/py/jingdutiao# python3 test5.py
100%|████████████████████████████████████████████████████████████████████████| 499/499 [00:05<00:00, 92.20it/s
方式4:
root@ubuntu16:/alex/py/jingdutiao# cat test2.py
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)
if __name__ == '__main__':
import time,os
pb = ProgressBar()
for i in range(101):
os.system('clear')
print( pb(i))
time.sleep(0.1)
root@ubuntu16:/alex/py/jingdutiao#
执行结果:
|#################################################-|
percent done
|##################################################| #输出100行内容,但是在屏幕会实时清屏,只展示1行
percent done
方式5:
root@ubuntu16:/alex/py/jingdutiao# python3 test3.py
====================================================================================================>100%
#cat test3.py
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)