Python Get Column Names from CSV File Tutorial

Published On: 10/01/2023 | Category: Python


Hi Dev,

In this example, you will learn how to get header from csv file in python. This article goes in detailed on python get column names from csv file. we will help you to give example of python csv get header names. I would like to share with you how to get column names from csv file in python. Alright, let’s dive into the steps.

We will use one demonstration demo.csv. ID, Name, and Email fields are included in the csv file. The reader(), next(), and open() functions will then be used to read the header from the csv file.

So let's see bellow example:

Example 1

main.py
from csv import reader
  
# skip first line from demo.csv
with open('demo.csv', 'r') as readObj:
  
    csvReader = reader(readObj)
    # Get CSV File Columns Name
    header = next(csvReader)
  
    print(header)
Output
['ID', 'Name', 'Email']

It will help you....