Skipping multiple lines in Python csv

I connect to the API to get some data. The result is a report that includes a multi-line header combined with a traditional single-line header.

Example:

1. Document Name: Test 2. Document Date: 8/7/2015 3. Document ID: 3804804 4. Document Author: Joe Blow 5. 6. Date, ID, Name, Age, Sex, Result 7. 8/7/2015, 2808380, Sara Jenkings, 33, F, 208.20 

In this example, I want to skip lines 1 - 5 and write a line in line 6 as a title line, and all other lines after that - as regular lines.

Now I know how to skip one line using next(reader, None) , but how to skip more than one line if I know that the number of skipped lines will be 5 lines, as in the example?

I would usually use a database to skip rows, but I want to see if I can get Python to save data correctly without doing any extra work with the database.

+8
source share
3 answers

You can use itertools.islice , passing in the line with which you want to start recording as the second parameter, so for line 6 based on 0, you use 5 , if stop is None, iteration continues until the iterator is exhausted.

 import csv from itertools import islice with open("in.csv") as f, open("out.csv","w") as out: r = csv.reader(islice(f, start=5,stop=None)) wr = csv.writer(out) wr.writerows(r) 

You do not necessarily need the csv module if you save the lines as is:

 with open("in.csv") as f, open("out.csv","w") as out: r = islice(f, 5 ,None) out.writelines(r) 
+11
source

You can add a counter and an if statement to the for loop.

 count = 0 for line in opened_file: if count < 5: count += 1 continue #Parse lines 
+2
source

Skipping 5 header lines using list comprehension:

 import csv nheaderlines = 5 with open(path + file) as csvfile: reader = csv.DictReader(csvfile) [next(reader, None) for item in range(nheaderlines)] for row in reader: print(row) 
0
source

All Articles