0

I've got a csv file with 5 colums. The fifth column is the one with a possible comment. Now I would like to extract only the rows of which the fifth column is filled. So from the example below only the fourth row (or fifth if header is included). Then I would like to write this row in a csv with the same format. Hope anyone can help.

location;datetime;WNS2186;WNS2186 quality;WNS2186 comments  
241-036-00021_polder;10-02-2017 15:40;-2.272;original reliable;  
241-036-00021_polder;10-02-2017 15:50;-2.272;original reliable;  
241-036-00021_polder;10-02-2017 16:00;-2.272;original reliable;  
241-036-00021_polder;10-02-2017 16:10;-2.272;original reliable;test comment  
241-036-00021_polder;10-02-2017 16:20;-2.272;original reliable;   
241-036-00021_polder;10-02-2017 16:30;-2.272;original reliable;
yash
  • 1,357
  • 2
  • 23
  • 34
dirrek
  • 1
  • Open your csv file, iterate over each line and check if line has comment. Also you can use python csv module – hamidfzm Oct 30 '17 at 21:48

1 Answers1

0

I would implement this as a generator function that yields back rows that meet your criteria.

import csv

def valid_rows(path):
    with open(path, 'r') as f:
        reader = csv.DictReader(f)
        yield from (row for row in reader if row['comments'])
ahawker
  • 3,306
  • 24
  • 23