I appologize if this was already answered before, but I checked I bunch of posts and just cannot understand what is wrong with my code. I'm trying to read a csv file in python (see bellow) and filter out rows of data by the value in the second column (angle). Then I want to create a new output file with filtered time and angle values. I only get the output file with headers written in.
csv file:
time,angle
0,56
1,89
2,112
3,189
4,122
5,123
Code:
import csv
#define the min and max value of angle
alpha_min = 110
alpha_max = 125
#read csv file and loop through with a filter
with open('test_csv.csv', 'r') as input_file:
csv_reader = csv.reader(input_file)#, delimiter=',')
#header = next(input_file).strip("\n").split(",")
results = filter(lambda row: alpha_min<row[1]<alpha_max, csv_reader)
#create output file
with open('test_output_csv.csv', "w") as output_file:
csv_writer = csv.writer(output_file, delimiter=',')
csv_writer.writerow(header)
for result in results:
csv_writer.writerow(result)