Skip first line(field) in loop using CSV file? [duplicate]
Solution 1:
There are many ways to skip the first line. In addition to those said by Bakuriu, I would add:
with open(filename, 'r') as f:
next(f)
for line in f:
and:
with open(filename,'r') as f:
lines = f.readlines()[1:]
Solution 2:
The best way of doing this is skipping the header after passing the file object to the csv
module:
with open('myfile.csv', 'r', newline='') as in_file:
reader = csv.reader(in_file)
# skip header
next(reader)
for row in reader:
# handle parsed row
This handles multiline CSV headers correctly.
Older answer:
Probably you want something like:
firstline = True
for row in kidfile:
if firstline: #skip first line
firstline = False
continue
# parse the line
An other way to achive the same result is calling readline
before the loop:
kidfile.readline() # skip the first line
for row in kidfile:
#parse the line
Solution 3:
csvreader.next() Return the next row of the reader’s iterable object as a list, parsed according to the current dialect.