Read file from line 2 or skip header row

How can I skip the header row and start reading a file from line2?


Solution 1:

with open(fname) as f:
    next(f)
    for line in f:
        #do something

Solution 2:

f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

Solution 3:

If you want the first line and then you want to perform some operation on file this code will helpful.

with open(filename , 'r') as f:
    first_line = f.readline()
    for line in f:
            # Perform some operations

Solution 4:

If slicing could work on iterators...

from itertools import islice
with open(fname) as f:
    for line in islice(f, 1, None):
        pass