先说一下文本系统的控制符:
\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实现进度条总结的更多相关文章

  1. Python字符进度条

    Python字符进度条 看看这个神奇的module from tqdm import trange from time import sleep for r in trange(10, 1, -1): ...

  2. Python之进度条及π的计算

    Python之进度条及π的计算 文本进度条 1.  简单的开始 这是利用print()函数来实现简单的非刷新文本进度条.它的基本思想是按照任务执行百分比将整个任务划分为100个单位,每执行N%输出一次 ...

  3. Python实现进度条功能

    Python实现进度条功能 import sys, time def progress(percent, width=50): # 设置进度条的宽度 if percent >= 100: # 当 ...

  4. Python实现进度条和时间预估的示例代码

    一.前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家 ...

  5. python print 进度条的例子

    def progress(width, percent): print "%s %d%%\r" % (('%%-%ds' % width) % (width * percent / ...

  6. python实现进度条

    先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...

  7. python实现进度条和百分比同时显示

    python中同时打印进度条和百分比 仅打印进度条: import sys,time for i in range(100): sys.stdout.write('>') sys.stdout. ...

  8. 自主学习python文本进度条及π的计算

    经过自己一段时间的学习,已经略有收获了!在整个过程的进行中,在我逐渐通过看书,看案例,做题积累了一些编程python的经验以后,我发现我渐渐爱上了python,爱上了编程! 接下来,当然是又一些有趣的 ...

  9. python显示进度条

    当一个python任务是需要逐个处理相同的事物时(里面有循环操作,例如对一系列的文件进行处理),这时可以将处理的进度条加进来,下面是一个例子: import time import sys def v ...

  10. 从 Python 第三方进度条库 tqdm 谈起 (转载)

    原文地址: https://blog.ernest.me/post/python-progress-bar tqdm 最近一款新的进度条 tqdm 库比较热门,声称比老版的 python-progre ...

随机推荐

  1. Mysql中FIND_IN_SET和REPLACE函数简介

    一  FIND_IN_SET() SELECT * from u_user where FIND_IN_SET('32',tags) 上面的sql是精确查找,查找表中tags中含有32的记录(注意这里 ...

  2. 本人遇到的spring事务之UnexpectedRollbackException异常解决笔记

    本人最近在使用spring事务管理的过程中遇到如下异常,导致服务端抛出500给前端,让搞前端的哥们抱怨我心里着实不爽,前前后后折腾了近半个小时才得于解决,今天就做个笔记,以免日后又犯这个错误.好了,错 ...

  3. 在Spring Boot中使用 @ConfigurationProperties 注解 (二十六)

    @ConfigurationProperties主要作用:就是绑定application.properties中的属性 java代码 @Configuration public class DataS ...

  4. 获得文件路径 _pgmptr, _makepath, _splitpath

    #include <stdlib.h> #include <stdio.h> int main(void) { char path_buffer[_MAX_PATH]; cha ...

  5. 转载:【Oracle 集群】RAC知识图文详细教程(九)--RAC基本测试与使用

    文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...

  6. java之double类型数值的比较

    先看demo: public class L26 { /** * @param args */ public static void main(String[] args) { // TODO Aut ...

  7. ES6介绍二 函数的增强

    ES6对于函数的使用新增了很多实用的API,JS的函数跟很多后台语言PHP,ASP.NET开始看齐: 1. 参数默认值: 以前我们为了给函数创建默认值,必须用一种冗杂的语句,而且有歧义的语句. //E ...

  8. ISE创建Microblaze软核(二)

    ISE创建Microblaze软核(二) (2012-07-13 15:09:08) 转载▼ 标签: 杂谈 分类: FPGA开发 第四步 进入Platform Studio操作界面 通过向导创建软核后 ...

  9. react 拖拽排序---原生

    定义css, 两个动画 .drag-up { -webkit-animation: dragup ease 0.2s 1; animation: dragup ease 0.2s 1; -webkit ...

  10. linux磁盘分区格式化-fdisk命令工具

    本文主要讲述使用fdisk工具对磁盘进行分区和格式化的方法 首先要明确分区是针对磁盘进行的操做,磁盘分区会创建分区表,类似vda,sda的是磁盘,vda1,sda1的是分区 1.查看磁盘分区状态 1. ...