Press "Enter" to skip to content

Python – How to Skip First Line in CSV?

In Python, while reading a CSV using the CSV module you can skip the first line using next() method. We usually want to skip the first line when the file is containing a header row, and we don’t want to print or import that row. The following is an example.

Suppose you have a CSV file containing the following data with a header line. You want to read and print the lines on screen without the first header line.

DEPTNO,DNAME,LOC
10,ACCOUNTING,NEW YORK
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON

Example to Skip First Line While Reading a CSV File in Python

Use next() method of CSV module before FOR LOOP to skip the first line, as shown in below example.

import csv

with open("product_record.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    # the below statement will skip the first row
    next(csv_reader)
    for lines in csv_reader:
      print(lines)

Output

['10', 'ACCOUNTING', 'NEW YORK']
['20', 'RESEARCH', 'DALLAS']
['30', 'SALES', 'CHICAGO']
['40', 'OPERATIONS', 'BOSTON']

See also:

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *