1

Part of my python program is to load the contents of a (csv) file into a list. Since this file is large, I'd like to add some progress data about it. This is what I'm doing right now:

def loadFile(infilepath):
    answer = []
    with open(infilepath) as infile:
        for progress,row in enumerate(csv.reader(infile), 1):
            if not progress%10000:
                progress = format(progress, ',')
                print(progress, '\b'*len(progress), sep='', end='', flush=True)
            answer.append(doStuffWith(row))
    return answer

print("Loading Data... ", end='', flush=True)
data = loadData('path/to/file')

Normally, I'd replace the print(progress, '\b'*len(progress), ...) with print(progress, '\r', ...) to bring the cursor back to the start of the line. But that's not an option here, as the start of line is offset by "Loading Data... ". I was told that I could use progressbar2 for this, but I can't seem to figure out how to make it write actual values, instead of a progress bar.

I'd appreciate any help on this

1 Answer 1

1

Use a custom widget:

import time
from progressbar import ProgressBar, FormatLabel, UnknownLength

def transform(n):
    return format(n, ',')

widget = FormatLabel('Loading Data... %(value)s')
widget.mapping = {**widget.mapping, 'value': ('value', transform)}
with ProgressBar(widgets=[widget], max_value=UnknownLength) as bar:
    for i in range(1, 1234):
        time.sleep(0.001)
        bar.update(i)
Sign up to request clarification or add additional context in comments.

11 Comments

I can't have 'Loading Data... ' as part of the the update message
Why not? What difference does it make whether this is part of the progress bar context or not?
It would require too much refactoring. Loading Data... isn't always the same - different functions that call this function print different string beforehand... and too many functions call this for me to refactor the function to take an extra parameter - I'd like to avoid that
Also, your solution doesn't work, as it doesn't update progress based on the input file, which has an unknown number of lines (usually much greater than 100). So bar.update(lineNumber) is a ValueError
Then you can't use \r, you have to keep track of the offset position of the start of line somehow. Either with your own idea (a bunch of \b characters) or learn curses.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.