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)
轉載于:https://www.cnblogs.com/fmgao-technology/p/9181855.html
總結
以上是生活随笔為你收集整理的Python实现进度条总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 题解:中位数
- 下一篇: python 错误、调试和测试