How to read CSV file in Python?

PythonServer Side ProgrammingProgramming

A CSV file stands for Comma Separated Values file. It is a plain text file in which data values are separated by commas and hence represent a tabular data in the form of plain text with the help of commas. A CSV file has .csv extension.

Here’s how a CSV files look like −

Sr.No,Name,City,Age
1,Rahul,Kolkata,21
2,Karan,Amritsar,23
3,Priya,Bangalore,20

To create a CSV file, you can simply write the file in above format in notepad and save it with .csv extension.

Read CSV file in Python

The csv file stored on your local storage in system can be read with the help of Python.

We need to import the csv module in Python. Then we need to open the file in read mode since we need to read the data from the file. The csv.reader() function is used to read the data from the CSV file. The csv.reader() returns an iterable reader object. We need to iterate over the returned iterable reader object to process the contents of the csv file.

Implementation

Let us have a csv file stored on our local system with the name “ttp.csv”. The contents of the file are displayed in the example above. We need to read the contents of this file and print the contents of the file line by line.

Example

import csv
with open('ttp.csv','r')as file:
   filecontent=csv.reader(file)
   for row in filecontent:
   print(row)

Note: Make sure the csv file and the Python program are at the same location in the system, i.e., the same folder.

Output

['Sr.No', 'Name', 'City', 'Age']
['1', 'Rahul', 'Kolkata', '21']
['2', 'Karan', 'Amritsar', '23']
['3', 'Priya', 'Bangalore', '20']

Explanation line by line

  • import csv − It is required to import the csv module in Python in order to use the functions included in this module to read the file.

  • open the file using open(). The open() takes two parameters, the name of the file and the mode in which you want to open it. Here the mode is ‘r’ since we need to read the file.

  • Read the contents of the file using csv.reader(). This returns an iterable reader object which is returned to a variable.

  • Iterate over the filecontents to print the file content row wise.

The csv file and the Python program reading it must be at the same location.

It is required that the csv file and Python program are at the same location or in the same folder in your local system. If the file is in some different location, it raises an exception and gives the following output.

Traceback (most recent call last):
   with open('ttp.csv','r')as file:
FileNotFoundError: [Errno 2] No such file or directory: 'ttp.csv'

Read CSV file with custom delimiter

The comma is not the only delimiter to be used in the csv files. Other delimiters used are : , ; ,| etc. If instead of comma, some other delimiter is used, we need to modify the code accordingly.

Implementation

Let in the above csv file, the delimiter used be ‘:’ instead of the comma. Thus the code will be modified as below −

Example

import csv
with open('ttp.csv','r')as file:
   filecontent=csv.reader(file,delimiter=':')
   for row in filecontent:
      print(row)

Output

['Sr', 'No', 'Name', 'City', 'Age']
['1', 'Rahul', 'Kolkata', '21']
['2', 'Karan', 'Amritsar', '23']
['3', 'Priya', 'Bangalore', '20']

The only modification in the code is to specify the delimiter used in the csv file in the csv.reader() . If the delimiter used is ‘|’, then this needs to be specified as the delimiter in the code.

If not specified, by default code will consider only comma as the delimiter and since comma is not present in the file, the whole row will be considered as a single item.

If the delimiter is not specified in the above code for the above file using ‘:’ delimiter, it produces the following output which is wrong.

['Sr:No:Name:City:Age']
['1:Rahul:Kolkata:21']
['2:Karan:Amritsar:23']
['3:Priya:Bangalore:20']

As in the above output, whole row is considered as a single item.

raja
Published on 11-Mar-2021 13:39:02
Advertisements