dimanche 31 mai 2020

Combining two "for" to integrate a progress bar while deleting line in a text file in Python?

I manage to make two python script to work independently. The first one is about looking for a string in a text file and deleting all lines with this string.

bad_words = ['first.com','second.org','third.io']

with open('input.txt') as oldfile, open('output.txt', 'w') as newfile:
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):
            newfile.write(line) 

The process is quite long because the input as nearly 1 000 000 lines and the bad_words are close to 100 entries.

So I would like to show a progress bar in the terminal while proceeding. I found this, and it is working, incrementing the bar every 1/10 of a second.

import time

# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
    """
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        length      - Optional  : character length of bar (Int)
        fill        - Optional  : bar fill character (Str)
        printEnd    - Optional  : end character (e.g. "\r", "\r\n") (Str)
    """
    percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
    filledLength = int(length * iteration // total)
    bar = fill * filledLength + '-' * (length - filledLength)
    print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = printEnd)
    # Print New Line on Complete
    if iteration == total: 
        print()



# A List of Items
items = list(range(0, 57))
l = len(items)

# Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for i, item in enumerate(items):
    # Do stuff...
    time.sleep(0.1)
    # Update Progress Bar
    printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', length = 50)

Instead of a time sleep, I want the progress bar to move forward while each word from bad_words is being processed.

So I came up with that :

def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):

    percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
    filledLength = int(length * iteration // total)
    bar = fill * filledLength + '-' * (length - filledLength)
    print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = printEnd)
    # Print New Line on Complete
    if iteration == total: 
        print()

items = ['first.com','second.org','third.io']
l = len(items)


printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
with open('A+B_net.txt') as oldfile, open('A+B_sub.txt', 'w') as newfile:
    for line in oldfile:
        if not any(items in line for item in items):
            newfile.write(line)
    for i, item in enumerate(items):
    # Update Progress Bar
        printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', length = 50)

It seems the imbrication of the "For If" aren't proper.

Aucun commentaire:

Enregistrer un commentaire