Open In App
Related Articles

Reading CSV files in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

A CSV (Comma Separated Values) file is a form of plain text document that 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 in Python that use either the CSV module or the pandas library. 

  • csv Module: The CSV module is one of the modules in Python that 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 provide 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 the reader() 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.

Example: This code reads and prints the contents of a CSV file named ‘Giants.csv’ using the csv module in Python. It opens the file in read mode, reads the lines, and prints them one by one using a for loop. The csv.reader() function is used to read the CSV file, and the data from each row is printed to the console.

Python3

import csv
with open('Giants.csv', mode ='r')as file:
  csvFile = csv.reader(file)
  for lines in csvFile:
        print(lines)

                    


Output:

['Organization', 'CEO', 'Established']
['Alphabet', 'Sundar Pichai', '02-Oct-15']
['Microsoft', 'Satya Nadella', '04-Apr-75']
['Amazon', 'Jeff Bezos', '05-Jul-94']

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 the DictReader class 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 consists of dictionary keys. 

Example: This code reads and prints the contents of a CSV file named ‘Giants.csv’ using the csv module with DictReader. It opens the file in read mode, reads the lines, and prints them one by one. csv.DictReader() reads the CSV file and treats the first row as headers, creating a dictionary for each row where the header values are the keys. The code prints each row as a dictionary, making it easier to work with structured CSV data.

Python3

import csv
with open('Giants.csv', mode ='r') as file:   
       csvFile = csv.DictReader(file)
       for lines in csvFile:
            print(lines)

                    


Output:

OrderedDict([(‘Organization’, ‘Alphabet’), (‘CEO’, ‘Sundar Pichai’), (‘Established’, ’02-Oct-15′)]) 
OrderedDict([(‘Organization’, ‘Microsoft’), (‘CEO’, ‘Satya Nadella’), (‘Established’, ’04-Apr-75′)]) 
OrderedDict([(‘Organization’, ‘Amazon’), (‘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.

Example: This code uses the pandas library to read and display the contents of a CSV file named ‘Giants.csv.’ It reads the CSV file and stores it as a DataFrame using the pandas.read_csv() function. Finally, it prints the entire DataFrame, which provides a structured and tabular representation of the CSV data. This is a common approach when working with tabular data in Python, as pandas offers powerful tools for data manipulation and analysis.

Python3

import pandas
csvFile = pandas.read_csv('Giants.csv')
print(csvFile)

                    


Output:

Organization            CEO Established
0 Alphabet Sundar Pichai 02-Oct-15
1 Microsoft Satya Nadella 04-Apr-75
2 Amazon Jeff Bezos 05-Jul-94

Note: To know more about pandas.csv_read() click here.



Last Updated : 21 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads