Skipping more than one row in Python csv
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)
You can use itertools.islice, passing the line you want to begin writing from as the second parameter so for line 6 being 0 based you use 5
, If stop is None, then 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 don't necessarily need the csv module if you are keeping the lines as is:
with open("in.csv") as f, open("out.csv","w") as out:
r = islice(f, 5 ,None)
out.writelines(r)
You can add a counter and an if statement to a for loop.
count = 0
for line in opened_file:
if count < 5:
count += 1
continue
#Parse lines