Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 554
user1067257 Avatar asked Dec 02 '25 06:12

user1067257


1 Answers

You could do:

reader = csv.reader(total)
all(next(reader) for i in range(4))

or

for i in range(4): next(reader)
like image 140
Joel Cornett Avatar answered Dec 04 '25 20:12

Joel Cornett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!