0

Is it possible to ignore or skip the first row in a CSV file with filter?

filtered = filter(lambda p: celery == p[7],reader)
csv.writer(open(filename, 'w',newline=''), delimiter=',').writerows(filtered)

I'm trying to read and filter only the 7th row containing the word celery but because the filter keeps reading the header stuff it messes up the line under it.

                                                          Stuff                                                                                                   
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato Celery Lettuce Carrot
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato Celery Lettuce Carrot
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato Celery Lettuce Carrot

I think because it can't find celery it just cuts off the line under it?

ge Cracker Honey Cheese Grape Bread Tomato Celery Lettuce Carrot
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato Celery Lettuce Carrot
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato Celery Lettuce Carrot
Vedang Mehta
  • 2,214
  • 9
  • 22
iOSecure
  • 232
  • 6
  • 18

1 Answers1

1

Just add next before you construct filtered:

Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

Example:

next(reader, None) # skips a line - default set to None
filtered = filter(lambda p: celery == p[7],reader)
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88