Get Column Names from CSV File in Python

28-Dec-2022

.

Admin

Get Column Names from CSV File in Python

Hi Dev,

I am going to show you example of get column names from csv file in python. I would like to share with you python get column names from csv file. you can understand a concept of how to get column names from csv file in python. I explained simply about how to get header from csv file in python. you will do the following things for python csv get header names.

In this example, we will take one demo.csv file with ID, Name and Email fields. Then, we will use open(), next() and reader() functions to read header from csv file.

Example :


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']

#Python