Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check conditions in several items in a row in Python?
    text
    copied!<p>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:</p> <pre><code> 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 </code></pre> <p>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 &lt;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.</p> <p>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: </p> <ol> <li>Enter the number of cycles to check...</li> <li>How many cycles in a row should meet the criteria...</li> </ol> <p>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.</p> <p>Here is my script:</p> <pre><code>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 &gt;= 9.00 and col5 &gt; 1.00: print(" %.0f " % col0,'|', "%.12f" % col4, "%.12f" % col5, "%.12f" % (col4/col5), "%.12f" % (100*col25), "%.12f" % col3, "%.12f" % col23, "%.12f" % col24) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload