import csv
with open('motion 2017-11-13 13-34-25.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['event'] == "location":
print(row['time'], row['event'], row['latitude'], row['longitude'] ) #column fields want to print
else:
if row['event'] == "motion":
print(row['time'], row['event'], row['earthAcceleration.x'],
row['earthAcceleration.y'], row['earthAcceleration.z'] ) #column fields i want
else:
print(row['time'], row['event'] )
I am using if else to display certain values according to action, now from 3 cases one case will be true and value will be printed, but I want to print that output in a csv file(write into it) Sample input
time event acceleration.x rotation.x latitude longitude
2017-11-20 11:22:21 153 location -0.901233 0.8397 1.121 23.22
2017-11-20 11:22:31 153 motion -0.541233 0.4327 2.22 30.22
2017-11-20 11:22:21 153 start -0.901233 0.8397 2.22 5.22
Sample output
time event latitude longitude
2017-11-20 11:22:21 153 location 1.121 23.22
So according to the condition(event) the row will be fetched if it is motion,location or anything else, the output should be that row and I want all those rows with that event to write into a csv file.