Reading Rows from a CSV File in Python
Last Updated :
20 Dec, 2021
CSV stands for Comma Separated Values. This file format is a delimited text file that uses a comma as a delimiter to separate the text present in it. Or in other words, CSV files are used to store data in tabular form. As per the name suggested, this file contains the data which is separated by a comma and every line of a file is considered a record. We can create the CSV file either from notepad or using excel.
Example:
Geeks,for,geeks
one,of,the,
top,learning,platform
We can create a CSV file using the following ways:
1. Using Notepad: We can create a CSV file using Notepad. In the Notepad, open a new file in which separate the values by comma and save the file with .csv extension.

2. Using Excel: We can also create a CSV file using Excel. In Excel, open a new file in which specify each value in a different cell and save it with filetype CSV.

To read data row-wise from a CSV file in Python, we can use reader and DictReader which are present in the CSV module allows us to fetch data row-wise.
Using reader
Using reader we can iterate between rows of a CSV file as a list of values. It iterates over all rows in a CSV file and fetches data in each row as a list. reader() method is present in CSV library. So to use this reader method, first we need to import the CSV library. reader object accepts a single parameter called fileObject (a variable that holds the CSV file).
Syntax:
csv.reader(fileobject)
Steps to read CSV file:
Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method.
with open('filename') as fileObject
While loading the file by specifying path along with filename, if you got any unicode error then append r before path of filename
with open(r'path/filename') as fileObject
Step 2: Create a reader object by passing the above-created file object to the reader function.
reader_obj = csv.reader(file_obj)
Step 3: Use for loop on reader object to get each row.
Example:
Consider a CSV file named "samplecsv.csv". This file contains the following data:
Id,Name,Rating
1,Akhil,4
2,Babu,3
3,Nikhil,5
Python3
# Python program to read CSV file line by line
# import necessary packages
import csv
# Open file
with open('samplecsv.csv') as file_obj:
# Create reader object by passing the file
# object to reader method
reader_obj = csv.reader(file_obj)
# Iterate over each row in the csv
# file using reader object
for row in reader_obj:
print(row)
Output:
['Id', 'Name', 'Rating']
['1', 'Akhil', '4']
['2', 'Babu', '3']
['3', 'Nikhil', '5']
Reading CSV file without header
Everything is fine with the above example but if we don't want column names to fetch or we can say we don't want to read the header of the file, then we use the next() method on the file object before creating the reader object so that it skips the headings.
Python3
# Python program to read CSV file without header
# Import necessary packages
import csv
# Open file
with open('samplecsv.csv') as file_obj:
# Skips the heading
# Using next() method
heading = next(file_obj)
# Create reader object by passing the file
# object to reader method
reader_obj = csv.reader(file_obj)
# Iterate over each row in the csv file
# using reader object
for row in reader_obj:
print(row)
Output:
['1', 'Akhil', '4']
['2', 'Babu', '3']
['3', 'Nikhil', '5']
Explanation: The above code is almost the same as the code in the above example but a slight change is we use the next function here that helps us to skip the column names while accessing data from a CSV file i.e. first row. If column names are required then we access them from the heading object i.e. return the result of the next method.
Using DictReader
When using a reader() method we can iterate over a CSV file as a list but using the DictReader class object we can iterate over a CSV file row by row as a dictionary. This DictReader method is present in the csv library. So to use it first we need to import the csv library. DictReader() accepts a single parameter called fileObject (a variable that holds the csv file).
Syntax
csv.DictReader(fileobject)
Steps to read CSV file:
Step 1: Load the CSV file using the open method in a file object.
with open('filename') as fileObject
Step 2: Create a reader object with the help of DictReader method using fileobject.
reader_obj = csv.DictReader(file_obj)
This reader object is also known as an iterator can be used to fetch row-wise data.
Step 3: Use for loop on reader object to get each row.
Example:
Python3
# Python3 program to read CSV file using DictReader
# Import necessary packages
import csv
# Open file
with open('samplecsv.csv') as file_obj:
# Create reader object by passing the file
# object to DictReader method
reader_obj = csv.DictReader(file_obj)
# Iterate over each row in the csv file
# using reader object
for row in reader_obj:
print(row)
Output:
OrderedDict([('Id', '1'), ('Name', 'Akhil'), ('Rating', '4')])
OrderedDict([('Id', '2'), ('Name', 'Babu'), ('Rating', '3')])
OrderedDict([('Id', '3'), ('Name', 'Nikhil'), ('Rating', '5')])
Explanation: In the code, first we loaded the CSV file named samplecsv.csv and then created a reader_object that can be iterated to fetch each row. The returned result is in the form of Key-Value pair indicates it as a dictionary. So using DictReader we read data row by row as a Dictionary.
Similar Reads
Computer Fundamentals Tutorial This Computer Fundamentals Tutorial covers everything from basic to advanced concepts, including computer hardware, software, operating systems, peripherals, etc. Whether you're a beginner or an experienced professional, this tutorial will enhance your computer skills and take them to the next level
4 min read
Fundamental
Computer HardwareComputer hardware refers to the physical components of a computer that you can see and touch. These components work together to process input and deliver output based on user instructions. In this article, weâll explore the different types of computer hardware, their functions, and how they interact
10 min read
What is a Computer Software?Computer Software serves as the backbone of all digital devices and systems. It is an integral part of modern technology. Unlike hardware which comprises physical components, software is intangible and exists as a code written in programming language. This article focuses on discussing computer soft
8 min read
Central Processing Unit (CPU)The Central Processing Unit (CPU) is like the brain of a computer. Itâs the part that does most of the thinking, calculating, and decision-making to make your computer work. Whether youâre playing a game, typing a school assignment, or watching a video, the CPU is busy handling all the instructions
6 min read
Input DevicesInput devices are important parts of a computer that help us communicate with the system. These devices let us send data or commands to the computer, allowing it to process information and perform tasks. Simply put, an input device is any tool we use to give the computer instructions, whether it's t
11 min read
Output DevicesOutput devices are hardware that display or produce the results of a computer's processing. They convert digital data into formats we can see, hear, or touch. The output device may produce audio, video, printed paper or any other form of output. Output devices convert the computer data to human unde
9 min read
Memory
Computer MemoryMemory is the electronic storage space where a computer keeps the instructions and data it needs to access quickly. It's the place where information is stored for immediate use. Memory is an important component of a computer, as without it, the system wouldnât operate correctly. The computerâs opera
9 min read
What is a Storage Device? Definition, Types, ExamplesThe storage unit is a part of the computer system which is employed to store the information and instructions to be processed. A storage device is an integral part of the computer hardware which stores information/data to process the result of any computational work. Without a storage device, a comp
11 min read
Primary MemoryPrimary storage or memory is also known as the main memory, which is the part of the computer that stores current data, programs, and instructions. Primary storage is stored in the motherboard which results in the data from and to primary storage can be read and written at a very good pace.Need of P
4 min read
Secondary MemorySecondary memory, also known as secondary storage, refers to the storage devices and systems used to store data persistently, even when the computer is powered off. Unlike primary memory (RAM), which is fast and temporary, secondary memory is slower but offers much larger storage capacities. Some Ex
7 min read
Hard Disk Drive (HDD) Secondary MemoryPrimary memory, like RAM, is limited and volatile, losing data when power is off. Secondary memory solves this by providing large, permanent storage for data and programs.A hard disk drive (HDD) is a fixed storage device inside a computer that is used for long-term data storage. Unlike RAM, HDDs ret
11 min read
Application Software
MS Word Tutorial - Learn How to Use Microsoft Word (2025 Updated)Microsoft Word remains one of the most powerful word processing program in the world. First released in 1983, this word processing software has grown to serve approximately 750 million people every month. Also, MS Word occupies 4.1% of the market share for productivity software.With features like re
9 min read
MS Excel Tutorial - Learn Excel Online FreeExcel, one of the powerful spreadsheet programs for managing large datasets, performing calculations, and creating visualizations for data analysis. Developed and introduced by Microsoft in 1985, Excel is mostly used in analysis, data entry, accounting, and many more data-driven tasks.Now, if you ar
11 min read
What is a Web Browser and How does it Work?The web browser is an application software used to explore the World Wide Web (WWW). It acts as a platform that allows users to access information from the Internet by serving as an interface between the client (user) and the server. The browser sends requests to servers for web documents and servic
4 min read
What is a Excel SpreadsheetExcel works like other spreadsheet programs but offers more features. Each Excel file is called a workbook, which contains one or more worksheets. You can start with a blank workbook or use a template.A worksheet is a grid of 1,048,576 rows and 16,384 columns, over 17 billion cells, for entering and
7 min read
System Software
Programming Languages
C Programming Language TutorialC is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Python Tutorial - Learn Python Programming LanguagePython is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Java TutorialJava is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read