Windows Python IDEL编译器自动忽略'\r'及简单进度条的实现
2015-12-24 23:28
302 查看
Windows环境下,Python自带的IDEL编译器在进行字符串输出时会忽略字符串结尾的'\r'
导致光标无法回到行首。
另在DOS窗口下采用Python print.py运行 运行的结果正确。
附简单进度条代码。
使用'\r'回到行首和sys.stdout.write/flush输出并刷新缓冲区。
注意特殊字符实心方块的编码方式。
导致光标无法回到行首。
另在DOS窗口下采用Python print.py运行 运行的结果正确。
附简单进度条代码。
使用'\r'回到行首和sys.stdout.write/flush输出并刷新缓冲区。
注意特殊字符实心方块的编码方式。
# coding=utf-8 import sys,time t = "▉" t = t.decode("utf-8") class ProgressBar: def __init__(self, count = 0, total = 0, width = 20): self.count = count self.total = total self.width = width def move(self): self.count += 1 def log(self, s): progress = self.width * self.count / self.total sys.stdout.write('{0:3}/{1:3}: \r'.format(self.count, self.total)) sys.stdout.flush() sys.stdout.write(t* progress + '--' * (self.width - progress)+'\r') print s if progress == self.width: sys.stdout.write('Done!') else : sys.stdout.write('Please waitting!\n\r') num = 10 bar = ProgressBar(total=num) for i in range(num): bar.move() bar.log('%2d'%((float)(i+1)/num*100)+'%\r') time.sleep(1)
相关文章推荐
- Python2.7 NumPy和PIL模块安装
- 零基础入门学习Python(19):对象(4)组合、绑定
- 进击python第一篇:相遇
- python之模块py_compile用法(将py文件转换为pyc文件)
- Python爬虫 - 登录csdn
- 使用Pycharm安装Python第三方库
- Python第三方库安装技巧
- 【转】Python计算器小程序及文件IO
- python之模块pprint之常见用法
- 为Python安装pip工具
- python之模块poplib之常见用法
- 为Python安装easy_install工具
- python中__getitem__, __setitem__, __delitem__的使用
- python面向对象具体解释(上)
- Python爬虫学习系列教程
- Python爬虫入门八之Beautiful Soup的用法
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- 【转】windows下python开发环境搭建
- python 生成 xml文件 属性的顺序问题
- python的urlparse