使用Python过滤csv文件中的行

时间:2013-10-23 15:45:04

标签: csv python-3.x

我有一个打开并修改文本文件的脚本。包含人员信息和午餐帐户余额的文本文件。我的脚本采用文本文件删除引号,只写第8列中包含值D,F或R的行。它将此过滤后的数据写入两个文件,一个名为lunchimport.csv的csv导入文件,用于单独的程序和csv调用临时文件以用于进一步过滤。脚本的第二阶段使用csv temp文件生成另外两个csv文件。一个文件negativebal.csv在第14列中只包含负值的行。另一个文件lowbal.cav包含第14列中值为0到5的行。我的问题是我无法让脚本过滤“正确”之间的值。当使用下面的代码只是在第14行中写入值为0到5之间的行时,不会过滤掉任何内容。如果我使用介于0和1.99之间的值,它就可以工作。任何大于1.99且代码都没有过滤任何东西:

if row[13] > "0" and row[13] < "1.99":
    lowwriter.writerow([row[0], row[13]])

我在下面粘贴了我的整个代码。我确实使用了很多临时文件来完成我的任务。可能有更好的方法,但我只是想让我的过滤器正常工作。

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] < "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] > "0" and row[13] < "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")

1 个答案:

答案 0 :(得分:0)

当您可能需要数字比较时,使用字符串进行比较:

if 0. < float(row[13]) < 1.99:
    lowwriter.writerow([row[0], row[13]])