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)
Python实现进度条总结的更多相关文章
- Python字符进度条
Python字符进度条 看看这个神奇的module from tqdm import trange from time import sleep for r in trange(10, 1, -1): ...
- Python之进度条及π的计算
Python之进度条及π的计算 文本进度条 1. 简单的开始 这是利用print()函数来实现简单的非刷新文本进度条.它的基本思想是按照任务执行百分比将整个任务划分为100个单位,每执行N%输出一次 ...
- Python实现进度条功能
Python实现进度条功能 import sys, time def progress(percent, width=50): # 设置进度条的宽度 if percent >= 100: # 当 ...
- Python实现进度条和时间预估的示例代码
一.前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家 ...
- python print 进度条的例子
def progress(width, percent): print "%s %d%%\r" % (('%%-%ds' % width) % (width * percent / ...
- python实现进度条
先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...
- python实现进度条和百分比同时显示
python中同时打印进度条和百分比 仅打印进度条: import sys,time for i in range(100): sys.stdout.write('>') sys.stdout. ...
- 自主学习python文本进度条及π的计算
经过自己一段时间的学习,已经略有收获了!在整个过程的进行中,在我逐渐通过看书,看案例,做题积累了一些编程python的经验以后,我发现我渐渐爱上了python,爱上了编程! 接下来,当然是又一些有趣的 ...
- python显示进度条
当一个python任务是需要逐个处理相同的事物时(里面有循环操作,例如对一系列的文件进行处理),这时可以将处理的进度条加进来,下面是一个例子: import time import sys def v ...
- 从 Python 第三方进度条库 tqdm 谈起 (转载)
原文地址: https://blog.ernest.me/post/python-progress-bar tqdm 最近一款新的进度条 tqdm 库比较热门,声称比老版的 python-progre ...
随机推荐
- Ubuntu16 nginx安装http_image_filter_module模块
目录 配置image_filter 配置 重启nginx 如何安装呢? 安装image_filter模块依赖的库. 查看之前的配置 添加上图片模块[由于它是系统模块,不需要额外下载,直接添加就可以了] ...
- git-it 教程,一些git知识点。/ 如何解决merge conflict/ 如何使用Github Pages./Git术语表
一个git使用教程 https://:.com/jlord/git-it-electron#what-to-install 一个在线Github的功能教学:https://lab.github.com ...
- jquery.validate验证表单
添加引用 <script src="/${appName}/commons/js/validate/jquery.validate.min.js"></scrip ...
- 前端构建工具 Grunt 入门
之前也介绍过前端构建工具 Ant 和 Yeoman,其中 Yeoman 工具就包含了 Grunt 所以就不多说.那么与 Ant 相比 Grunt 有这么几个优点: Javascript 语法,相比 A ...
- lvs+keepalived+bind实现负载均衡高可用智能dns【转】
转:https://www.cnblogs.com/mikeluwen/p/7068356.html 整体架构: 1.IP地址规划: Dns1:172.28.0.54 Dns2:172.28.0.55 ...
- IOS UI-QQ好友列表
一.Model // // FriendsModel.h // IOS_0111_好友列表 // // Created by ma c on 16/1/11. // Copyright (c) 201 ...
- 阿里云服务器购买 发布web项目全过程
http://blog.csdn.net/liona_koukou/article/details/50496946
- 旋转木马幻灯片切换效果JS源码详解
首先,放上慕课网的课程链接,源码是在这个课程里分享出来的,https://www.imooc.com/learn/386. 文章适合学习过这个课程的同学,再看这篇文章,可能有更深入的理解.主要是对各种 ...
- 在jenkins和sonar中集成jacoco(三)--使用jacoco收集集成测试的覆盖率
我们系统使用weblogic做服务器,集成测试框架使用的是junit+selenium. 首先,要把jacoco的jacocoagent.jar包放到部署应用的服务器上,接着在系统服务的JAVA_OP ...
- 64位的ubuntu14.04 LTS安装 Linux交叉编译工具链及32位“ia32-libs”依赖库
ubuntu又迎来了其新一代的长期支持版本 14.04 LTS,其带来了许多令人期待的新特新,遂决定进行升级. 装好了64位版本及安装 Linux交叉编译工具链 运行GCC,${CROSS_COMPI ...