Search code examples
pythoncsvdelete-row

Strip the First Four Rows of a CSV in Python?


I have a batch of 50-60 csv files which, for whatever reason, have total junk data for the first four rows of each file. After the junk data, however, the column headers are properly listed, and the rest of the file is fine. How could I go about stripping each file of these first four files in python? Here is my code thus far:

import csv
total = open('C:\\Csv\\201.csv', 'rb')
for row in csv.reader(total):
    print row

As you can see, all I have done is opened the file and printed its contents. I have searched around for solutions of deleting certain aspects of csv files, but most either delete entire columns, or hinge on a particular condition for the row to be deleted. In my case, it is simply a matter of order, and every file needs to be stripped of its first four rows. Any and all help is greatly appreciated.


Solution

  • for i, line in enumerate(sys.stdin, -4):
        if i>=0: print line,