A CSV (Comma Separated Values) file is a form of plain text document which uses a particular format to organize tabular information. CSV file format is a bounded text document that uses a comma to distinguish the values. Every row in the document is a data log. Each log is composed of one or more fields, divided by commas. It is the most popular file format for importing and exporting spreadsheets and databases.
Reading a CSV File
There are various ways to read a CSV file that uses either the csv module or the pandas library.
- csv Module: The CSV module is one of the modules in Python which provides classes for reading and writing tabular information in CSV file format.
- pandas Library: The pandas library is one of the open-source Python libraries that provides high-performance, convenient data structures and data analysis tools and techniques for Python programming.
Reading a CSV File Format in Python:
Consider the below CSV file named ‘Giants.CSV’:

- USing csv.reader(): At first, the CSV file is opened using the
open()method in ‘r’ mode(specifies read mode while opening a file) which returns the file object then it is read by using thereader()method of CSV module that returns the reader object that iterates throughout the lines in the specified CSV document.Note: The ‘
with‘ keyword is used along with the open() method as it simplifies exception handling and automatically closes the CSV file.importcsv# opening the CSV filewithopen('Giants.csv', mode='r')asfile:# reading the CSV filecsvFile=csv.reader(file)# displaying the contents of the CSV fileforlinesincsvFile:print(lines)chevron_rightfilter_noneOutput:
['Organiztion', 'CEO', 'Established'] ['Alphabet', 'Sundar Pichai', '02-Oct-15'] ['Microsoft', 'Satya Nadella', '04-Apr-75'] ['Aamzon', 'Jeff Bezos', '05-Jul-94']
In the above program reader() method is used to read the Giants.csv file which maps the data into lists.
- Using csv.DictReader() class: It is similar to the previous method, the CSV file is first opened using the
open()method then it is read by using theDictReaderclass of csv module which works like a regular reader but maps the information in the CSV file into a dictionary. The very first line of the file comprises of dictionary keys.importcsv# opening the CSV filewithopen('Giants.csv', mode='r') asfile:# reading the CSV filecsvFile=csv.DictReader(file)# displaying the contents of the CSV fileforlinesincsvFile:print(lines)chevron_rightfilter_noneOutput:
OrderedDict([(‘Organiztion’, ‘Alphabet’), (‘CEO’, ‘Sundar Pichai’), (‘Established’, ’02-Oct-15′)])
OrderedDict([(‘Organiztion’, ‘Microsoft’), (‘CEO’, ‘Satya Nadella’), (‘Established’, ’04-Apr-75′)])
OrderedDict([(‘Organiztion’, ‘Aamzon’), (‘CEO’, ‘Jeff Bezos’), (‘Established’, ’05-Jul-94′)])- Using pandas.read_csv() method: It is very easy and simple to read a CSV file using pandas library functions. Here
read_csv()method of pandas library is used to read data from CSV files.importpandas# reading the CSV filecsvFile=pandas.read_csv('Giants.csv')# displaying the contents of the CSV fileprint(csvFile)chevron_rightfilter_noneOutput:
Organiztion CEO Established 0 Alphabet Sundar Pichai 02-Oct-15 1 Microsoft Satya Nadella 04-Apr-75 2 Aamzon Jeff Bezos 05-Jul-94
In the above program, the csv_read() method of pandas library reads the Giants.csv file and maps its data into a 2D list.
Note: To know more about pandas.csv_read() click here.
Recommended Posts:
- Reading and Writing CSV Files in Python
- Python program to read CSV without CSV module
- Working with csv files in Python
- Writing CSV files in Python
- How to skip rows while reading csv file using Pandas?
- Ways to import CSV files in Google Colab
- Creating a dataframe using CSV files
- Reading and Writing to text files in Python
- Python | Reading .ini Configuration Files
- Reading and Writing XML Files in Python
- Python | Read csv using pandas.read_csv()
- Convert CSV to Excel using Pandas in Python
- Convert JSON to CSV in Python
- Saving Text, JSON, and CSV to a File in Python
- Convert CSV to HTML Table in Python
- Writing data from a Python List to CSV row-wise
- Convert HTML table into CSV file in python
- Load CSV data into List and Dictionary using Python
- Create a GUI to convert CSV file into excel file using Python
- Convert CSV to JSON using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
- Using csv.DictReader() class: It is similar to the previous method, the CSV file is first opened using the

