Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python enumerate() tqdm progress-bar when reading a file?

I can't see the tqdm progress bar when I use this code to iterate my opened file:

        with open(file_path, 'r') as f:         for i, line in enumerate(tqdm(f)):             if i >= start and i <= end:                 print("line #: %s" % i)                 for i in tqdm(range(0, line_size, batch_size)):                     # pause if find a file naed pause at the currend dir                     re_batch = {}                     for j in range(batch_size):                         re_batch[j] = re.search(line, last_span) 

what's the right way to use tqdm here?

like image 742
Wei Wu Avatar asked Jan 25 '18 06:01

Wei Wu


People also ask

What is tqdm progress bar?

A tqdm progress bar gives information such as. The Number and Percentage of iterations completed out of the total number of iterations. Total Elapsed Time. Estimated Time to Complete the loop, and. The speed of the loop in iterations per second (or it/s).

What is tqdm () in Python?

tqdm is a library in Python which is used for creating Progress Meters or Progress Bars. tqdm got its name from the Arabic name taqaddum which means 'progress'. Implementing tqdm can be done effortlessly in our loops, functions or even Pandas.

Does tqdm work with while loops?

tqdm does not require any dependencies and works across multiple python environments. Integrating tqdm can be done effortlessly in loops, on iterable, with Pandas or even with machine learning libraries— just wrap any iterable with tqdm(iterable) , and you're done!


2 Answers

You're on the right track. You're using tqdm correctly, but stop short of printing each line inside the loop when using tqdm. You'll also want to use tqdm on your first for loop and not on others, like so:

with open(file_path, 'r') as f:     for i, line in enumerate(tqdm(f)):         if i >= start and i <= end:             for i in range(0, line_size, batch_size):                 # pause if find a file naed pause at the currend dir                 re_batch = {}                 for j in range(batch_size):                     re_batch[j] = re.search(line, last_span) 

Some notes on using enumerate and its usage in tqdm here.

like image 128
Valentino Constantinou Avatar answered Sep 18 '22 16:09

Valentino Constantinou


I ran into this as well - tqdm is not displaying a progress bar, because the number of lines in the file object has not been provided.

The for loop will iterate over lines, reading until the next newline character is encountered.

In order to add the progress bar to tqdm, you will first need to scan the file and count the number of lines, then pass it to tqdm as the total

from tqdm import tqdm  num_lines = sum(1 for line in open('myfile.txt','r')) with open('myfile.txt','r') as f:     for line in tqdm(f, total=num_lines):         print(line) 
like image 43
user1446308 Avatar answered Sep 16 '22 16:09

user1446308