Chapter: Working with CSV Files (Class XII CS - CBSE)
✅ 1. What is a CSV File?
CSV stands for Comma Separated Values.
It is a plain text file that contains tabular data.
Each line of the file is a data record.
Each record consists of fields, separated by commas.
📌 Example:
Name, Age, Class
Anjali, 17, XII
Rahul, 16, XII
✅ 2. Features / Characteristics of CSV Files
Lightweight and easy to create/edit using any text editor.
Stores data in tabular form.
Each line = one record.
Comma (,) is the default delimiter (can be changed).
No support for complex data types (only strings or numbers).
Easily readable by both humans and programs.
✅ 3. Python Library Used
import csv
The csv module is used to work with CSV files.
✅ 4. CSV File Modes in Python
Mode Description
'r' Read (default)
'w' Write (overwrites existing file)
'a' Append (adds to the end)
'r+' Read and Write
✅ 5. Important Functions in csv Module
Function Description
csv.reader() Reads CSV file line by line
csv.writer() Writes to a CSV file
writerow() Writes a single row
writerows() Writes multiple rows
next() Reads next row from csv.reader
csv.DictReader() Reads CSV into dictionary format
csv.DictWriter() Writes dictionary into CSV
✅ 6. Types of Q/A
📌 A. Definitions
Q1: What is a CSV file?
A: A CSV (Comma Separated Values) file is a plain text file that stores
tabular data, where each line is a row and each field is separated by a
comma.
Q2: What is the purpose of csv.writer()?
A: It is used to write data into a CSV file in Python.
📌 B. Differences
Q1: Difference between text file and CSV file
Text File CSV File
Stores data as plain text Stores tabular data
May have unstructured content Structured: rows and columns
No specific delimiter Default delimiter is comma (,)
Needs custom parsing Easily parsed using csv module
📌 C. Features / Characteristics Based Qs
Q1: State any two features of CSV files.
A:
1. Stores data in tabular format.
2. Fields are separated by commas.
📌 D. Functions Based
Q1: What does writerow() do?
A: It writes a single row into the CSV file.
Q2: What is the use of csv.DictReader()?
A: It reads each row in the CSV file as a dictionary.
📌 E. Library / Module Based
Q1: Which module is used to work with CSV files in Python?
A: csv module.
Q2: What is the difference between csv.reader() and csv.DictReader()?
A:
csv.reader() returns a list of strings.
csv.DictReader() returns a dictionary for each row using the
header as keys.
✅ 7. Python Code Examples
📄 Example 1: Writing to a CSV file
import csv
with open("students.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age", "Class"])
writer.writerow(["Amit", 17, "XII"])
writer.writerow(["Neha", 18, "XII"])
📄 Example 2: Reading from a CSV file
import csv
with open("students.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
📄 Example 3: Using DictWriter
import csv
data = [
{"Name": "Amit", "Age": 17, "Class": "XII"},
{"Name": "Neha", "Age": 18, "Class": "XII"},
]
with open("students_dict.csv", "w", newline="") as file:
fieldnames = ["Name", "Age", "Class"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
📄 Example 4: Using DictReader
import csv
with open("students_dict.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row["Name"], row["Age"])
✅ 8. Practical Use Case
CSV files are commonly used in:
Storing data from web forms.
Exporting/importing data from databases or spreadsheets.
Exchanging data between applications