Signup/Sign In

How to Read a File from Line 2 or Skip the Header Row?

In this article, we will learn how one can read a file from the second line in Python. We will use some built-in functions, some simple approaches, and some custom codes as well to better understand the topic.

Python handles various file operations. In the case of reading files, the user can start reading a file either from the first-line or from the second line. This article will show how you can skip the header row or the first line and start reading a file from line 2. Let us discuss four different methods to read a file from line 2. We will read a sample.txt file as well as a sample.csv file.

Sample Text File //sample.txt

Student Details of Class X
David, 18, Science
Amy, 19, Commerce
Frank, 19, Commerce
Mark, 18, Arts
John, 18, Science

Sample CSV File //sample.csv

Student Details of Class X
David  18 Science
Amy    19 Commerce
Frank  19 Commerce
Mark   18 Arts
John   18 Science

Now, let us look at four different ways to read a text file and a csv file from line 2 in Python. We will use the above files to read the contents.

Example: Read the Text File from Line 2 using next()

We use the sample.txt file to read the contents. This method uses next() to skip the header and starts reading the file from line 2.

Note: If you want to print the header later, instead of next(f) use f.readline() and store it as a variable or use header_line = next(f). This shows that the header of the file is stored in next().

#opens the file
with open("sample.txt") as f:
    #start reading from line 2
    next(f)
    for line in f:
        print(line)

#closes the file
f.close()


David, 18, Science
Amy, 19, Commerce
Frank, 19, Commerce
Mark, 18, Arts
John, 18, Science

Example: Read the Text File from Line 2 using readlines()

We use the sample.txt file to read the contents. This method uses readlines() to skip the header and starts reading the file from line 2. readlines() uses the slicing technique. As you can see in the below example, readlines[1:] , it denotes that the reading of the file starts from index 1 as it skips the index 0. This is a much more powerful solution as it generalizes to any line. The drawback of this method is that it works fine for small files but can create problems for large files. Also, it uses unnecessary space because slice builds a copy of the contents.

#opens the file
f = open("sample.txt",'r')

#skips the header
lines = f.readlines()[1:]
print(lines)

#closes the file
f.close()


['David, 18, Science\n', 'Amy, 19, Commerce\n', 'Frank, 19, Commerce\n', 'Mark, 18, Arts\n', 'John, 18, Science']

Example: Read the Text File from Line 2 using islice()

We use the sample.txt file to read the contents. This method imports islice from itertools module in Python. islice() takes three arguments. The first argument is the file to read the data, the second is the position from where the reading of the file will start and the third argument is None which represents the step. This is an efficient and pythonic way of solving the problem and can be extended to an arbitrary number of header lines. This even works for in-memory uploaded files while iterating over file objects.

from itertools import islice

#opens the file
with open("sample.txt") as f:
     for line in islice(f, 1, None):
        print(line)

#closes the file
f.close()


David, 18, Science
Amy, 19, Commerce
Frank, 19, Commerce
Mark, 18, Arts
John, 18, Science

Example: Read the CSV File from Line 2

We use the sample.csv file to read the contents. This method reads the file from line 2 using csv.reader that skips the header using next() and prints the rows from line 2. This method can also be useful while reading the content of multiple CSV files.

import csv

#opens the file
with open("sample.csv", 'r') as r: 
        next(r)                  
        #skip headers             
        rr = csv.reader(r)
        for row in rr:
            print(row)


['David', '18', 'Science']
['Amy', '19', 'Commerce']
['Frank', '19', 'Commerce']
['Mark', '18', 'Arts']
['John', '18', 'Science']

Conclusion

In this article, we learned to read file contents from line 2 by using several built-in functions such as next(), readlines(), islice(), csv.reader() and different examples to skip the header line from the given files.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.