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 ...
随机推荐
- c语言 找最小值
#include <stdio.h> #define N 10 #define MIN(X,Y) ((X<Y)?(X):(Y)) int f(int arr[],int len,in ...
- python 列表元素替换以及删除
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', ...
- redis高可用 - Master&Slave
Master&Slave也就是我们所说的主从复制,即主机数据更新后根据配置和策略,自动同步到备机的机制.其中Master以写为主,Slave以读为主. Master&Slave的作用主 ...
- 【Mysql】修改mysql的字符集和默认存储引擎,解决数据入库乱码问题
背景 在使用Python + Testlink做自动化的过程中,遇到了数据入库出现乱码的情况,后来通过修改字符集的方式解决了这个问题.下面的内容主要来自于一篇相关博文,博主做了细微的调整, 原文链接: ...
- 过滤器系列(三)—— RSQF
这个过滤器本身是一篇论文中提出的过滤器的简化版本,去掉了计数功能,我觉得简化版本应用的可能也很广,专门写一篇简化版本的RSQF.RSQF全称是rank-and-select based filter, ...
- 046——VUE中组件之使用动态组件灵活设置页面布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Missing artifact com.sun:tools:jar:1.7解决方案
在配置Java + Robotframework时遇到的问题“Missing artifact com.sun:tools:jar” 1. 先检查一下eclipse或STS中的JDK路径配置是否正确( ...
- linux内存查看工具
这里帮你总结了一下Linux下查看内存使用情况的多种方法~ 在做 Linux 系统优化的时候,物理内存是其中最重要的一方面.自然的,Linux 也提供了非常多的方法来监控宝贵的内存资源的使用情况.下面 ...
- chrome 49 版本bug: flex父元素设置flex:1 , 子元素用height:100%无法充满父元素
1 <div class="container"> <div class="item"> <div class="ite ...
- Django 之 Ajax
此次主要是做省市区的三级联动. 环境:django 1.10 1. urls.py # coding:utf-8 from django.conf.urls import url import vie ...