Computer Science

CSV File Handling in Python Class 12 CBSE (083): Complete Notes with Reader and Writer Methods

Class 12 · Computer Science

CSV File Handling in Python (CBSE Class 12 Computer Science - 083)

CSV (Comma-Separated Values) files are one of the most commonly used file formats for storing tabular data. Each line in a CSV file represents a record, and the values in each record are separated by commas. Python provides the built-in csv module to efficiently read and write CSV files.

Learning Objectives

  • Understand CSV files and their applications.
  • Import the csv module.
  • Open and close CSV files.
  • Write data using writer(), writerow(), and writerows().
  • Read data using the reader() method.

What is a CSV File?

A CSV (Comma-Separated Values) file is a text file used to store tabular data such as student records, employee details, sales reports, and marks. Each row represents one record, and each value is separated by a comma.

Example File: student.csv

Contents of student.csv


RollNo,Name,Marks
101,Riya,95
102,Aman,88
103,Neha,91

Advantages of CSV Files

  • Simple and easy to understand.
  • Stores tabular data efficiently.
  • Can be opened in spreadsheet software such as Microsoft Excel and LibreOffice Calc.
  • Consumes less storage space.
  • Easy to exchange data between different applications.

Importing the CSV Module

Before working with CSV files, the csv module must be imported.

Syntax


import csv

Opening a CSV File

A CSV file is opened using the open() function, just like a text file.

Syntax


file = open("student.csv","r")

Closing a CSV File

After performing file operations, always close the file to release system resources.

Syntax


file.close()

Example


file = open("student.csv","r")

file.close()

Writing Data to a CSV File

Python uses the writer() method to create a writer object, which is then used to write data into the CSV file.

Syntax


writer_object = csv.writer(file)

The writer() Method

The writer() method creates a CSV writer object that provides methods to write records into the file.

Example


import csv

file = open("student.csv","w",newline="")

writer = csv.writer(file)

file.close()
Important: Use newline="" while opening a CSV file in write mode to prevent blank lines between records.

The writerow() Method

The writerow() method writes a single row into the CSV file.

Syntax


writer.writerow(record)

Example


import csv

file = open("student.csv","w",newline="")

writer = csv.writer(file)

writer.writerow(["RollNo","Name","Marks"])

writer.writerow([101,"Riya",95])

writer.writerow([102,"Aman",88])

file.close()

Contents of student.csv


RollNo,Name,Marks
101,Riya,95
102,Aman,88

The writerows() Method

The writerows() method writes multiple rows at once.

Syntax


writer.writerows(records)

Example


import csv

file = open("student.csv","w",newline="")

writer = csv.writer(file)

records = [

["RollNo","Name","Marks"],

[101,"Riya",95],

[102,"Aman",88],

[103,"Neha",91]

]

writer.writerows(records)

file.close()

Reading Data from a CSV File

Python uses the reader() method to read records from a CSV file.

Syntax


reader_object = csv.reader(file)

The reader() Method

The reader() method returns each row as a list.

Example


import csv

file = open("student.csv","r")

reader = csv.reader(file)

for row in reader:

    print(row)

file.close()

Output


['RollNo', 'Name', 'Marks']
['101', 'Riya', '95']
['102', 'Aman', '88']
['103', 'Neha', '91']

Using the with Statement

The with statement automatically closes the file after completing the operation.

Example


import csv

with open("student.csv","r") as file:

    reader = csv.reader(file)

    for row in reader:

        print(row)

Summary of CSV Methods

Method Purpose
csv.writer() Creates a writer object.
writer.writerow() Writes one row.
writer.writerows() Writes multiple rows.
csv.reader() Reads rows from a CSV file.

Common Programming Errors

  • Forgetting to import the csv module.
  • Not using newline="" while writing CSV files, resulting in blank lines.
  • Using writerow() instead of writerows() for multiple records.
  • Trying to write data without creating a writer object.
  • Forgetting to close the CSV file.

Exam Tips

  • Always remember to import the csv module before using CSV functions.
  • Use csv.writer() to create a writer object.
  • writerow() writes a single record, while writerows() writes multiple records.
  • csv.reader() returns each row as a list.
  • Use newline="" when opening a CSV file in write mode to avoid blank lines.
  • Practice programs for writing and reading CSV files, as they are commonly asked in CBSE practical examinations.

Frequently Asked Questions (FAQs)

1. What is a CSV file?

A CSV file stores tabular data where values are separated by commas.

2. Which module is used to work with CSV files?

The csv module is used for reading and writing CSV files.

3. What is the purpose of csv.writer()?

The csv.writer() method creates a writer object used to write data into a CSV file.

4. What is the difference between writerow() and writerows()?

writerow() writes one row at a time, whereas writerows() writes multiple rows in a single operation.

5. What does csv.reader() return?

The csv.reader() method returns each row of the CSV file as a list of strings.


Summary

  • A CSV file stores data in a tabular format using commas as separators.
  • The csv module provides functions to read and write CSV files.
  • csv.writer() creates a writer object.
  • writerow() writes a single record.
  • writerows() writes multiple records.
  • csv.reader() reads records one row at a time and returns them as lists.
  • Use newline="" while writing CSV files to avoid blank lines.
  • The with statement is the preferred way to open CSV files because it automatically closes the file.