idle和python区别_使用tqdm时,cmd和idle之间的区别是什么?

本文探讨了在IDLE和CMD中使用tqdm库创建进度条时出现的不同输出现象。在CMD中,进度条正常更新,而在IDLE中则逐行显示。原因在于IDLE和CMD对控制字符(如' ')的处理方式不同。为使IDLE显示类似CMD的效果,可以修改写入文件的方式,避免使用' '。此外,还讨论了开发环境中控制字符的适切使用。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

recently I want to add a simple progress bar to my script, I use tqdm to that, but what puzzle me is that the output is different when I am in the IDLE or in the cmd

for example this

from tqdm import tqdm

import time

def test():

for i in tqdm( range(100) ):

time.sleep(0.1)

give the expected output in the cmd

30%|███ | 30/100 [00:03<00:07, 9.14it/s]

but in the IDLE the output is like this

0%| | 0/100 [00:00<?, ?it/s]

1%|1 | 1/100 [00:00<00:10, 9.14it/s]

2%|2 | 2/100 [00:00<00:11, 8.77it/s]

3%|3 | 3/100 [00:00<00:11, 8.52it/s]

4%|4 | 4/100 [00:00<00:11, 8.36it/s]

5%|5 | 5/100 [00:00<00:11, 8.25it/s]

6%|6 | 6/100 [00:00<00:11, 8.17it/s]

7%|7 | 7/100 [00:00<00:11, 8.12it/s]

8%|8 | 8/100 [00:00<00:11, 8.08it/s]

9%|9 | 9/100 [00:01<00:11, 8.06it/s]

10%|# | 10/100 [00:01<00:11, 8.04it/s]

11%|#1 | 11/100 [00:01<00:11, 8.03it/s]

12%|#2 | 12/100 [00:01<00:10, 8.02it/s]

13%|#3 | 13/100 [00:01<00:10, 8.01it/s]

14%|#4 | 14/100 [00:01<00:10, 8.01it/s]

15%|#5 | 15/100 [00:01<00:10, 8.01it/s]

16%|#6 | 16/100 [00:01<00:10, 8.00it/s]

17%|#7 | 17/100 [00:02<00:10, 8.00it/s]

18%|#8 | 18/100 [00:02<00:10, 8.00it/s]

19%|#9 | 19/100 [00:02<00:10, 8.00it/s]

20%|## | 20/100 [00:02<00:09, 8.00it/s]

21%|##1 | 21/100 [00:02<00:09, 8.00it/s]

22%|##2 | 22/100 [00:02<00:09, 8.00it/s]

23%|##3 | 23/100 [00:02<00:09, 8.00it/s]

24%|##4 | 24/100 [00:02<00:09, 8.00it/s]

25%|##5 | 25/100 [00:03<00:09, 8.00it/s]

26%|##6 | 26/100 [00:03<00:09, 8.00it/s]

27%|##7 | 27/100 [00:03<00:09, 8.09it/s]

28%|##8 | 28/100 [00:03<00:09, 7.77it/s]

29%|##9 | 29/100 [00:03<00:09, 7.84it/s]

30%|### | 30/100 [00:03<00:08, 7.89it/s]

31%|###1 | 31/100 [00:03<00:08, 7.92it/s]

32%|###2 | 32/100 [00:03<00:08, 7.94it/s]

33%|###3 | 33/100 [00:04<00:08, 7.96it/s]

34%|###4 | 34/100 [00:04<00:08, 7.97it/s]

35%|###5 | 35/100 [00:04<00:08, 7.98it/s]

36%|###6 | 36/100 [00:04<00:08, 7.99it/s]

37%|###7 | 37/100 [00:04<00:07, 7.99it/s]

38%|###8 | 38/100 [00:04<00:07, 7.99it/s]

39%|###9 | 39/100 [00:04<00:07, 8.00it/s]

40%|#### | 40/100 [00:04<00:07, 8.00it/s]

41%|####1 | 41/100 [00:05<00:07, 8.00it/s]

I also get the same result if I make my own progress bar like

import sys

def progress_bar_cmd(count,total,suffix="",*,bar_len=60,file=sys.stdout):

filled_len = round(bar_len*count/total)

percents = round(100*count/total,2)

bar = "#"*filled_len + "-"*(bar_len - filled_len)

file.write( "[%s] %s%s ...%s\r"%(bar,percents,"%",suffix))

file.flush()

for i in range(101):

time.sleep(1)

progress_bar_cmd(i,100,"range 100")

why is that????

and there is a way to fix it???

解决方案

Limiting ourselves to ascii characters, the program output of your second code is the same in both cases -- a stream of ascii bytes representing ascii chars. The language definition does not and cannot specify what an output device or display program will do with the bytes, in particular with control characters such as '\r'.

The Windows Command Prompt console at least sometimes interprets '\r' as 'return the cursor to the beginning of the current line without erasing anything'.

In a Win10 console:

>>> import sys; out=sys.stdout

>>> out.write('abc\rdef')

def7

However, when I run your second code, with the missing time import added, I do not see the overwrite behavior, but see the same continued line output as with IDLE.

C:\Users\Terry>python f:/python/mypy/tem.py

[------------------------------------------------------------] 0.0% ...range 100[#-----------------------------------------------------------] ...

On the third hand, if shorten the write to file.write("[%s]\r"% bar), then I do see one output overwritten over and over.

The tk Text widget used by IDLE only interprets \t and \n, but not other control characters. To some of us, this seems appropriate for a development environment, where erasing characters is less appropriate than in a production environment.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值