How to Read CSV File Specific Column in Python?

29-Dec-2022

.

Admin

How to Read CSV File Specific Column in Python?

Hi Dev,

In this article we will cover on how to implement how to read csv file specific column in python. this example will help you python read csv file specific row. if you have question about python read csv file specific column data then I will give simple example with solution. This article will give you simple example of how to get specific row data from csv file in python.

In this example, we will take one demo.csv file with ID, Name and Email columns. Then, we will use open() and DictReader() functions to read specific column data from csv file.

Example :


main.py

from csv import reader

from csv import DictReader

# open demo.csv file in read mode

with open('demo.csv', 'r') as readObj:

# Pass the file object to DictReader() to get the DictReader object

csvDictReader = DictReader(readObj)

# get over each line as a ordered dictionary

for row in csvDictReader:

# Get ID and Name Columns only from CSV File

print(row['ID'], row['Name'])

Output:

1 Hardik Savani

2 Vimal Kashiyani

3 Harshad Pathak

#Python