Python Forum
Variable flag vs code outside of for loop?(Disregard)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable flag vs code outside of for loop?(Disregard)
#1
Big Grin 
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..
Reply
#2
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.
Reply
#3
yep, forgot how cool pandas was.. currently using it and about to post another extension question in my post from yesterday.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 427 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 406 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 526 May-23-2022, 08:53 AM
Last Post: bowlofred
  How to save specific variable in for loop in to the database? ilknurg 1 535 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 747 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Using Excel Cell As A Variable In A Loop knight2000 7 2,357 Aug-25-2021, 12:43 PM
Last Post: snippsat
  Using Excel Cell As A Variable In A Loop knight2000 7 2,863 Jul-18-2021, 10:52 AM
Last Post: knight2000
  Simple Variable Saving in Loop DevDev 3 2,128 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  While Loop Variable Freezing? stylingpat 13 3,955 Feb-25-2021, 10:42 AM
Last Post: Abdullah
  Change variable value during a while loop? penahuse 2 2,515 Nov-15-2020, 11:53 PM
Last Post: penahuse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020