使用tqdm时,cmd和idle之间的区别是什么? [英] what is the difference between cmd and idle when using tqdm?

查看:964
本文介绍了使用tqdm时,cmd和idle之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我想为我的脚本添加一个简单的进度条,我使用 tqdm ,但是什么困惑我的输出是不同的,当我在IDLE或在cmd



中,例如此

  tqdm 
导入时间

def test():
for i in tqdm(range(100)):
time.sleep(0.1)



在cmd中提供预期输出

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

输出如下

  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.12 / 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]

同样的结果,如果我自己进度条像

  import sys 

def progress_bar_cmd ,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%范围(101)中的i:
time.sleep(1)
progress_bar_cmd(1)
i,100,range 100)

strong>



并且有一种方法可以解决它

解决方案

将自己限制为ascii字符,第二个代码的程序输出在两种情况下都是相同的 - ascii字节流表示ascii字符。语言定义不能也不能指定输出设备或显示程序将对字节做什么,特别是使用诸如'\r'之类的控制字符。



Windows命令提示符控制台至少有时将'\r'解释为'将光标返回到当前行的开头而不删除任何东西'。
在Win10控制台中:

 > import sys; out = sys.stdout 
>>>> out.write('abc\rdef')
def7

你的第二个代码,缺少时间导入添加,我没有看到覆盖行为,但看到与IDLE一样的连续行输出。

  C:\Users\Terry> python f:/python/mypy/tem.py 
[--------------------- ---------------------------------------] 0.0%... range 100 [# - -------------------------------------------------- --------] ...

第三方面,如果缩短写到 file.write([%s] \r%bar),然后我看到一个输出重写覆盖。



IDLE使用的tk Text小部件仅解释\t和\\\
,而不解释其他控制字符。对于我们中的一些人,这似乎适合于开发环境,其中擦除字符不太适合在生产环境中。


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.

这篇关于使用tqdm时,cmd和idle之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆