Chinaunix首页 | 论坛 | 博客
  • 博客访问: 798918
  • 博文数量: 50
  • 博客积分: 757
  • 博客等级: 上士
  • 技术积分: 1913
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-29 14:29
个人简介

DBA

文章分类

全部博文(50)

文章存档

2015年(3)

2014年(2)

2013年(14)

2012年(19)

2011年(12)

分类: Python/Ruby

2013-07-23 16:47:24

进度条和一般的print区别在哪里呢?

答案就是print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,而且保证我们在下面看到最新的输出结果。

进度条不然,我们必须再原地输出才能保证他是一个进度条,否则换行了怎么还叫进度条?

最简单的办法就是,再输出完毕后,把光标移动到行首,继续在那里输出更长的进度条即可实现,新的更长的进度条把旧的短覆盖,就形成了动画效果。

可以想到那个转义符了吧,那就是 \r。

转义符\r就可以把光标移动到行首而不换行,转义符\n就把光标移动到行首并且换行。

在python中,输出stdout(标准输出)可以使用sys.stdout.write
例如:


  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. #Using GPL v2
  4. #Author: ihipop@gmail.com
  5. ##2010-10-27 22:07
  6. """
  7. Usage:
  8. Just A Template
  9. """
  10. from __future__ import division

  11. import sys,time
  12. j = '#'
  13. if __name__ == '__main__':
  14.     for i in range(1,61):
  15.         j += '#'
  16.         sys.stdout.write(str(int((i/60)*100))+'% ||'+j+'->'+"\r")
  17.         sys.stdout.flush()
  18.         time.sleep(0.5)
  19. print

第二种思路是用转义符\b
转义符\b是退格键,也就是说把输出的光标往回退格子,这样就可以不用+=了,例如:

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. #Using GPL v2
  4. #Author: ihipop@gmail.com
  5. #2010-10-27 22:07
  6. """
  7. Usage:
  8. Just A Template
  9. """
  10. from __future__ import division

  11. import sys,time
  12. if __name__ == '__main__':
  13.     for i in range(1,61):
  14.         sys.stdout.write('#'+'->'+"\b\b")
  15.         sys.stdout.flush()
  16.         time.sleep(0.5)
  17. print

光标回退2格,写个#再回退,再写,达到增长的目的了


不过写这么多似乎是废话,在耳边常常听到一句话:那就是不要重复造轮子。实际上python有丰富发lib帮你实现这个东西,你完全可以把心思放在逻辑开发上而不用注意这些小细节


下面要介绍的就是这个类“”(),使用可以方便的安装这可个类库,其实就一个文件,拿过来放到文件同一个目录下面也直接可以import过来

下面就是基本使用举例:


  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. #Using GPL v2
  4. #Author: ihipop@gmail.com
  5. #2010-10-27 22:53
  6. """
  7. Usage:
  8. Just A Template
  9. """
  10. from __future__ import division

  11. import sys,time
  12. from progressbar import *
  13. total = 1000

  14. #基本用法
  15. progress = ProgressBar()
  16. for i in progress(range(total)):
  17.   time.sleep(0.01)

  18. pbar = ProgressBar().start()
  19. for i in range(1,1000):
  20.     pbar.update(int((i/(total-1))*100))
  21.     time.sleep(0.01)
  22. pbar.finish()

  23. #高级用法
  24. widgets = ['Progress: ', Percentage(), ' ', Bar(marker=RotatingMarker('>-=')),
  25.            ' ', ETA(), ' ', FileTransferSpeed()]
  26. pbar = ProgressBar(widgets=widgets, maxval=10000000).start()
  27. for i in range(1000000):
  28.   # do something
  29.   pbar.update(10*i+1)
  30.   time.sleep(0.0001)
  31. pbar.finish()

官方示例下载,移步这里:


再发一个类:


  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. #Using GPL v2
  4. #Author: ihipop@gmail.com
  5. #2010-10-30 13:59
  6. """
  7. Usage:
  8. Just A Template
  9. """
  10. class progressbarClass:
  11.     def __init__(self, finalcount, progresschar=None):
  12.         import sys
  13.         self.finalcount=finalcount
  14.         self.blockcount=0
  15.         #
  16.         # See if caller passed me a character to use on the
  17.         # progress bar (like "*"). If not use the block
  18.         # character that makes it look like a real progress
  19.         # bar.
  20.         #
  21.         if not progresschar: self.block=chr(178)
  22.         else: self.block=progresschar
  23.         #
  24.         # Get pointer to sys.stdout so I can use the write/flush
  25.         # methods to display the progress bar.
  26.         #
  27.         self.f=sys.stdout
  28.         #
  29.         # If the final count is zero, don't start the progress gauge
  30.         #
  31.         if not self.finalcount : return
  32.         self.f.write('\n------------------- % Progress -------------------\n')
  33.         return

  34.     def progress(self, count):
  35.         #
  36.         # Make sure I don't try to go off the end (e.g. >100%)
  37.         #
  38.         count=min(count, self.finalcount)
  39.         #
  40.         # If finalcount is zero, I

Author Info :
  • From:Python中如何写控制台进度条的整理
  • URL:http://blog.ihipop.info/2010/10/1736.html
  • Please Reserve This Link,Thanks!
  • 阅读(3539) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~