Pasha
Pasha

Reputation: 179

How to check conditions in several items in a row in Python?

I am building a sorting script in Python. Having not that much of programming in the past I experience some difficulties. I have lots of CSV files that I open one by one using glob function. Then I need to sort and print the rows that meet certain cryteria. Here is a simplified version of the data:

   Col1    Col2 ....Col 2n
Row 1   30     0.25
Row 2   57     0.11
Row 3   100    0.24
Row 4   55     0.26
Row 5   60     0.28
...................
Row n   11     0.36

I got my script working fine when checking all the rows, but I need to include one more condition. I only want to print the rows that follow one after another in a sequence. I mean if, lets say, I want to print three rows that has Col 1 > 50. So, if Col_1 is >50 in the first row, second row and third row i print those rows. But if Col_1>50 in Row 1 but <50 in Row2 I will have to skip those rows and start with the next row. Sorry if I get you a bit confused. So, basically I need to print all the rows that meet the criteria in a row. In the example above, with condition Col1>50 I need to print three rows that meet that criteria in a row. So the script should print Rows 2-4.

More stuff: Ideally I would like to the script to take the user's input on how many rows should meet the criteria. So, if a user enters 4 - four rows in a row should meet the criteria, 10 - then all 10 should meet the criteria. Also, i would like to have input on how many rows to check. So basically:

  1. Enter the number of cycles to check...
  2. How many cycles in a row should meet the criteria...

Later on I will try to implement it in some GUI interface where number of cycles and conditions could be chosen from a drop down list.

Here is my script:

csvfiles = glob.glob('/stats/*.stat')
for filename in csvfiles: 
    reader = csv.reader(open(filename))
    for row in reader:                               
            col0, col3, col4, col5, col23, col24, col25 = float(row[0]), float(row[3]), float(row[4]), float(row[5]), float(row[23]), float(row[24]), float(row[25]) 
            if col4 >= 9.00 and col5 > 1.00:
               print("   %.0f   " % col0,'|', "%.12f" % col4, "%.12f" % col5, "%.12f" % (col4/col5), "%.12f" % (100*col25), "%.12f" % col3, "%.12f" % col23, "%.12f" % col24)

Upvotes: 2

Views: 1793

Answers (1)

user2461391
user2461391

Reputation: 1433

Add a counter and two arrays to store the previous rows. Here's some pseudocode to get you started

csvfiles = glob.glob('/stats/*.stat')
for filename in csvfiles:
    reader = csv.reader(open(filename))
    counter = 0
    temp1 = []
    temp2 = []
    for row in reader:
            col0, col3, col4, col5, col23, col24, col25 = float(row[0]), float(row[3]), float(row[4]), float(row[5]), float(row[23]), float(row[24]), float(row[25])
            if ###YOUR CONDITION IS TRUE
                counter += 1
            else
                counter = 0
            if col4 >= 9.00 and col5 > 1.00 and counter >= 3:
                if counter == 3
                    #PRINT TEMP1
                    #PRINT TEMP2
               print("   %.0f   " % col0,'|', "%.12f" % col4, "%.12f" % col5, "%.12f" % (col4/col5), "%.12f" % (100*col25), "%.12f" % col3, "%.12f" % col23, "%.12f" % col24)
            elif counter == 1
                #store in temp1
            elif counter == 2
                #store in temp2

The counter keeps track of how many rows in order meet the requirements. If it hits three, the previous two rows are printed and the current row is printed. After three, it just prints the current row. If the condition is false, counter gets set back to 0 and won't print anything until it reaches three again.

Upvotes: 1

Related Questions