Python Forum
Variable flag vs code outside of for loop?(Disregard) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Variable flag vs code outside of for loop?(Disregard) (/thread-36671.html)



Variable flag vs code outside of for loop?(Disregard) - cubangt - Mar-16-2022

No need for flag, mode code around and works like a champ. Big Grin

So i have the following logic and would like to add a flag or setup in a way that once the for loop is complete, then re-open the new file to remove key rows

    for line in file_in:
        match = datetime_rx.match(line)
        if match:
            file2.write(str(match.groups()[0]) + ',' + match.groups()[1] + '\n')
        else:
            print(line)
Is it better to have some variable that is set once the "if match" is complete or is it safe to just put the next logic outside of the "for line in file_in:"

This above logic, goes thru my raw data file and removed rows that are invalid and creates a new file with just valid rows, now from that file, i need to remove certain rows based on another post i did yesterday..


RE: Variable flag vs code outside of for loop?(Disregard) - deanhystad - Mar-16-2022

Are you using pandas for this now? Looks like it would be a great fit. Read file. Filter out non-matching elements and write to file. Filter out remaining elements before the date cutoff, write to a different file.


RE: Variable flag vs code outside of for loop?(Disregard) - cubangt - Mar-16-2022

yep, forgot how cool pandas was.. currently using it and about to post another extension question in my post from yesterday.