CSV File in Python Class 12 Notes

What is CSV File

  • A Comma Separated Values (CSV) file is a plain text file that contains the comma (,) separated data.
  • These files are often used for exchanging data between different applications.
  • CSV files are usually created by programs that handle huge amounts of data. They are used to export data from spreadsheets (ex:- excel file) and databases (Ex:- Oracle, MySQL). It can be used to import data into a spreadsheet or a database.


CSV File Structure
Name, DOB, City
Ram, 12-Jul-2001, Delhi
Mohan, 23-Jan-2005, Delhi


Python CSV Module

  • CSV Module is available in Python Standard Library.
  • The CSV module contains classes that are used to read and write tabular form of data into CSV format.
  • To work with CSV Files, programmer have to import CSV Module.
    import csv

Methods of CSV Module :

  1. writer( )
  2. reader( )

Both the methods return an Object of writer or reader class. Writer Object again have two methods –

  1. writerow( )
  2. writerows( )


writer( ) Methods

This function returns a writer object which is used for converting the data given by the user into delimited strings on the file object.

writer( ) Object Methods –

  • w_obj . writerow( <Sequence> ) : Write a Single Line
  • w_obj . writerows ( <Nested Sequence> ) : Write Multiple Lines


writerow() Example:-

import csv
row=[‘Nikhil’, ‘CEO’, ‘2’, ‘9.0’]
f=open(“myfile.csv”, ‘w’)
w_obj = csv.writer(f)


writerows() Example:

import csv
rows = [[‘Nikhil’,’CEO’,’2′,’9.0′],
w_obj = csv.writer(f)


reader( ) Methods

This function returns a reader object which will be used to iterate over lines of a given CSV file.

r_obj = csv.reader(csvfile_obj)

To access each row, we have to iterate over this Object.
for i in r_obj:

import csv
r_obj = csv.reader(f)
for data in r_obj:

By cbsepython

A complete solution for the students of class 9 to 12 having subject Information Technology (402), Computer Science (083). Explore our website for all the useful content as Topic wise notes, Solved QNA, MCQs, Projects and Quiz related to the latest syllabus.

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved