How can I use the csv module to remove all rows where any of the column values is missing? I my case I have two columns a and b, so if I have a value on a but nothing on b, that row should be removed. How an I achieve this?
Asked
Active
Viewed 63 times
-1
-
This is far too vague/broad. – AMC Dec 15 '19 at 10:26
-
Does this answer your question? [Deleting rows with Python in a CSV file](https://stackoverflow.com/questions/29725932/deleting-rows-with-python-in-a-csv-file) – snakecharmerb Dec 15 '19 at 10:29
1 Answers
2
Try this:
with open(in_filename) as infile:
with open(out_filename, 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
writer.writerows(filter(all, reader))

Alex Hall
- 34,833
- 5
- 57
- 89