Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the progress bar gracefully in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, Xiaobian will bring you about how to use the progress bar elegantly in Python. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

tqdm means "progress" in Arabic. The tqdm module allows you to show the progress of a loop very elegantly in the form of a progress bar.

By simply nesting tqdm on an iterable object, you can implement a progress bar that monitors loops.

The progress bar is displayed by default in the standard error stream, the sys.stderr file stream.

Here are some silly examples that show you the elegance and charm of tqdm modules.

Example 1: Basic example import time

from tqdm import tqdm

for i in tqdm(range(100)):

time.sleep(0.1)

Example 2: Add description [object Object] Example 3: Manually update import time

from tqdm import tqdm

with tqdm(total=100) as pbar:

for i in range(10):

time.sleep(0.1)

pbar.update(10)

Appendix: Manual implementation of loop progress bar import sys,time

#Progress bar function

def progress_bar(num,total):

rate = float(num)/total

ratenum = int(100*rate)

r = '\r[{}{}]{}%'.format('*'*ratenum,' '*(100-ratenum),ratenum)

sys.stdout.write(r)

sys.stdout.flush()

i,n = 0,100

for i in range(n):

time.sleep(0.1)

progress_bar(i+1,n)

The above is how to use the progress bar elegantly in Python shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report