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;
1
  • Open your csv file, iterate over each line and check if line has comment. Also you can use python csv module Commented Oct 30, 2017 at 21:48

1 Answer 1

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'])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.