Python实现进度条总结
来源:互联网 发布:淘宝店铺推广方法大全 编辑:程序博客网 时间:2024/06/14 14:37
找了一半天,始终没有找到自己满意的进度条。皇天不负有心人,找到了以为博主总结的进度条。
我的python是3.6,windows10系统,在Spyder运行成功!
先说一下文本系统的控制符:\r: 将光标移动到当前行的首位而不换行;\n: 将光标移动到下一行,并不移动到首位;\r\n: 将光标移动到下一行首位。 环境:root@ubuntu16:/alex/py/jingdutiao# python3Python 3.5.2 (default, Jul 5 2016, 12:43:10)[GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.>>> 方式1:root@ubuntu16:/alex/py/jingdutiao# cat test1.py#!/usr/bin/env pythonfrom __future__ import divisionimport sys,timej = '#'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.py98% ############################################################-> 方式2:root@ubuntu16:/alex/py/jingdutiao# cat test4.py#!/usr/bin/env python# -*- coding:utf-8 -*-__author__ = 'l' import sysfrom time import sleepdef 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.pycomplete percent ----->:100% 方式3:tqdm模块 tqdm是一个快速、扩展性强的进度条工具库, 其githup地址:https://github.com/tqdm/tqdm 1)安装:直接使用pip安装: pip install tqdm2)使用:from time import sleepfrom tqdm import tqdmfor i in tqdm(range(1, 500)): sleep(0.01) 自己实操:在ubuntu上默认安装到2.7环境变量里去了root@ubuntu16:/alex/py/jingdutiao# pip install tqdmCollecting tqdm Downloading tqdm-4.8.4-py2.py3-none-any.whlInstalling collected packages: tqdmSuccessfully installed tqdm-4.8.4You 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 pippip install tqdmpip -Vcd /usr/local/lib/python2.7/dist-packages/cp -r tqdm tqdm-4.8.4.dist-info/ /usr/local/lib/python3.5/dist-packagesroot@ubuntu16:/alex/py/jingdutiao# cat test5.pyfrom time import sleepfrom tqdm import tqdmfor i in tqdm(range(1, 500)): sleep(0.01)root@ubuntu16:/alex/py/jingdutiao# python3 test5.py100%|████████████████████████████████████████████████████████████████████████| 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,ospb = 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.pyimport sysimport timedef 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.flushif __name__ == '__main__': for i in range(0, 101): time.sleep(0.1) view_bar(i, 100)
阅读全文
0 0
- Python实现进度条总结
- python实现进度条
- python实现的控制台进度条
- python实现命令行下载进度条
- Python实现控制台中的进度条
- 飘逸的python - 实现控制台进度条效果
- python实现socket上传下载文件-进度条显示
- Python案例-开发之路-进度条实现
- python进度条
- 进度条总结
- 进度条实现
- 进度条实现
- python实现进度条--主要用在上传下载文件
- python用tqdm库来实现进度条功能
- Python使用一个进度条
- Python简单进度条示例
- python 进度条 tqdm
- Python进度条(tqdm)
- miniblink重新开更啦
- 【关系是双人舞,两个人跳得开心才…
- Http协议
- git合并多个提交
- 【一个杀人犯的养成:无法向父母撒…
- Python实现进度条总结
- 剑指Offer—51—构建乘积数组
- 【依靠自己可以获得幸福的人生吗?…
- 【一个痛苦迷茫的年轻人的困惑】人…
- 【同步荡秋千让学前儿学会合作】说…
- JAVA设计模式(十九)----观察者模式
- 【不要拿生命來開玩笑】「老婆,你…
- 【忧鬱症、专注力不集中,和饮食也…
- 基数排序(radixSort)