[英]progress bar in script Python
I have to ask something that other guys did, but I haven't find an answer (maybe is stupid).我不得不问其他人做过的事情,但我还没有找到答案(也许是愚蠢的)。
I need to implement a progress bar in my script python, ia red about tqdm and i like a lot that library because it has nice colours and the bar is well-structured, instead of a simple [---------].我需要在我的脚本 python 中实现一个进度条,即关于 tqdm 的红色,我很喜欢那个库,因为它有漂亮的颜色,而且进度条结构良好,而不是一个简单的 [--- ]。
a simple script that I saw is this:我看到的一个简单的脚本是这样的:
from tqdm import tqdm
for i in tqdm(range(10000)):
pass
but my question is: where do I insert my code??但我的问题是:在哪里插入我的代码? seems stupid but I tried several times and the bar won't go.
看起来很愚蠢,但我尝试了几次,但酒吧不会去。 simply stays at 0% and until my script ends.
只是保持在 0%,直到我的脚本结束。
Another point is that if I try also that simple code, everytime the bar update itself, it creates a new line with a new bar.另一点是,如果我也尝试那个简单的代码,每次栏更新自身时,它都会创建一个带有新栏的新行。 so how can I have a single bar that fill when time passes?
那么当时间过去时,我怎么能有一个填充的条形呢?
thanks a lot, and of course evry kind of support and advice will be appreciated非常感谢,当然,每一种支持和建议将不胜感激
EDIT---- If my code is:编辑----如果我的代码是:
alphabetic = ['a','b','c','d','e','f','g','h','i','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0-9']
for lett in alphabetic: lista_pag_success = pr.find_following_page('/artisti/lettera/'+lett)
artisti=[]
linksss=[]
for i in lista_pag_success:
artisti, linksss = pr.crawl_canzoni_it(i)
I have to put all this code in the tqdm() function?我必须把所有这些代码都放在 tqdm() 函数中吗? I have to pass just an iterable?
我必须通过一个迭代? but what if I have I case like this?
但是如果我有这样的情况怎么办? lista_pag_success and artisti, linksss are three lists - this is a code for a simple crawler –
lista_pag_success和artisti,linksss是三个list——这是一个简单爬虫的代码——
Your iterable over which you want to monitor progress, should replace range(10000)
in the sample code you posted.您想要监控进度的迭代应该替换您发布的示例代码中的
range(10000)
。
For example, if you have a list of numbers, [1, 4, 9, 10]
, and you want to find the square of each number, you would iterate over tqdm([1, 4, 9, 10])
and perform your calculation inside the for
loop, ie:例如,如果您有一个数字列表
[1, 4, 9, 10]
,并且您想找到每个数字的平方,您将迭代tqdm([1, 4, 9, 10])
并执行您在for
循环中的计算,即:
squared_nums = []
for num in tqdm([1, 4, 9, 10]):
squared_nums.append(num * num)
print(squared_nums)
This would generate output like:这将生成如下输出:
100%|████████████████████████████████████████████████████| 4/4 [00:00<00:00, 14820.86it/s]
[1, 16, 81, 100]
Of course, in this simple case, it could also be written using a list comprehension:当然,在这种简单的情况下,也可以使用列表推导式来编写:
squared_nums = [num * num for num in tqdm([1, 4, 9, 10])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.