Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltering rows in a csv file with Python
    text
    copied!<p>I have a script that opens and modifies a text file. The text file which contains personnel info and a lunch account balance. My script takes the text file removes the quotes and only writes rows that contain the values D, F or R in column 8. It writes this filtered data to two files, a csv import file called lunchimport.csv for a separate program and a csv temp file called to be used for further filtering. The second stage of the script uses the csv temp file to generate two additional csv files. One file, negativebal.csv, contains only rows with a negative value in column 14. The other file, lowbal.cav, contains rows with a value between 0 and 5 in column 14. My issue is that I cant get the script to filter "between" values properly. When using the code below to just write rows with values in column14 between 0 and 5 nothing will filter out. If I use values between 0 and 1.99 it works. Anything greater than 1.99 and the code doesnt filter anything: </p> <pre><code>if row[13] &gt; "0" and row[13] &lt; "1.99": lowwriter.writerow([row[0], row[13]]) </code></pre> <p>I have pasted my entire code below. I do use alot of temp files to accomplish my tasks. There probably is a better way but im just interested in getting my filters to work properly. </p> <pre><code>import os import csv infile = open("\\\\comalexsrv\\export\\update.txt", "r") outfile1 = open("casttemp1.csv", "w") infile2 = open("casttemp1.csv", "r") outfile2 = open("casttemp2.csv", "w") infile3 = open("casttemp2.csv", "r") outfile3 = open("casttemp3.csv", "w") infile4 = open("casttemp3.csv", "r") inowcsv = open("F:\zbennett\Lunch_Imports\lunchimport.csv", "w") negcastcsv = open("\\\\tcdc\\inow_transfer$\\negativebal.csv", "w") lowcastcsv = open("\\\\tcdc\\inow_transfer$\\lowbal.csv", "w") # Remove quotes in update.txt, write to outfile1(casttemp1.csv) string = infile.read() outfile1.write(string.replace("\"", '')) # Open infile2(casttemp1.csv), write rows with D,F,R in column 8 to outfile2(casttemp2.csv) # Open infile2(casttemp1.csv), write rows with D,F,R in column 8 to inowcsv(F:\zbennett\Lunch_Imports\lunchimport.csv) # Open infile2(casttemp1.csv), write rows with D,R in column 8 to outfile3(casttemp3.csv) tempwriter = csv.writer(outfile2, delimiter=',', lineterminator= '\n') importwriter = csv.writer(inowcsv, delimiter=',', lineterminator= '\n') lowtemp = csv.writer(outfile3, delimiter=',', lineterminator= '\n') for row in csv.reader(infile2, delimiter=','): if row[7] == "D": tempwriter.writerow(row) importwriter.writerow(row) lowtemp.writerow(row) if row[7] == "F": tempwriter.writerow(row) importwriter.writerow(row) if row[7] == "R": tempwriter.writerow(row) importwriter.writerow(row) lowtemp.writerow(row) # Open infile3(casttemp2.csv), write columns 1,14 for rows with less than 0 in column 14 to negcastcsv(\\tcdc\inow_transfer$\negativebal.csv) negwriter = csv.writer(negcastcsv, delimiter=',', lineterminator= '\n') for row in csv.reader(infile3, delimiter=','): if row[13] &lt; "0": negwriter.writerow([row[0], row[13]]) # Open infile4(casttemp3.csv), write columns 1,14 for rows with column 14 greater than 0 and less than 1.75 to lowcastcsv(\\tcdc\inow_transfer$\lowbal.csv) lowwriter = csv.writer(lowcastcsv, delimiter=',', lineterminator= '\n') for row in csv.reader(infile4, delimiter=','): if row[13] &gt; "0" and row[13] &lt; "1.99": lowwriter.writerow([row[0], row[13]]) infile.close() outfile1.close() infile2.close() outfile2.close() inowcsv.close() outfile3.close() infile3.close() infile4.close() negcastcsv.close() lowcastcsv.close() # Delete casttemp1.csv file os.remove("casttemp1.csv") os.remove("casttemp2.csv") os.remove("casttemp3.csv") </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