How to find specific rows in csv document in python -
what i'm trying read csv document , find values in sn column > 20 , make new file rows sn > 20.
i know need do:
- read original file
- open new file
- iterate on rows of original file
what i've been able find rows have value of sn > 20
import csv import os os.chdir("c:\users\robert\documents\qwe") open("gdweights_feh_robert_cmr.csv",'rb') f: reader = csv.reader(f, delimiter= ',') zerovar = 0 row in reader: if zerovar==0: zerovar = zerovar + 1 else: sn = row [11] zerovar = zerovar + 1 x = float(sn) if x > 20: print x
so question how take rows sn > 20 , turn new file?
save data in list, write list file.
import csv import os os.chdir(r"c:\users\robert\documents\qwe") output_ary = [] open("gdweights_feh_robert_cmr.csv",'rb') f: reader = csv.reader(f, delimiter= ',') zerovar = 0 row in reader: if zerovar==0: zerovar = zerovar + 1 else: sn = row [11] zerovar = zerovar + 1 x = float(sn) if x > 20: print x output_ary.append(row) open("output.csv",'w') f2: row in output_ary: item in row: f2.write(item + ",")
Comments
Post a Comment