[go: up one dir, main page]

0% found this document useful (0 votes)
6 views11 pages

CSV File Handling

The document provides an overview of CSV file handling in Python, explaining what a CSV file is and how to read and write them using the built-in csv module. It includes examples of using csv.reader, csv.writer, csv.DictReader, and csv.DictWriter for various operations. Additionally, it highlights important points to remember when working with CSV files and presents practice questions for students.

Uploaded by

izharluvcat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

CSV File Handling

The document provides an overview of CSV file handling in Python, explaining what a CSV file is and how to read and write them using the built-in csv module. It includes examples of using csv.reader, csv.writer, csv.DictReader, and csv.DictWriter for various operations. Additionally, it highlights important points to remember when working with CSV files and presents practice questions for students.

Uploaded by

izharluvcat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CSV FILE HANDLING

CLASS: XII (C.B.S.E.)

BY:
SANJEEV BHADAURIA
PGT CS (KV DUG)
📘 Chapter: CSV File Handling in Python
📌 What is a CSV File?
CSV stands for Comma Separated Values. It is a simple file format used to store
tabular data, such as a spreadsheet or database.
•Each line in the file is a row.
•Each value is separated by a comma (or any delimiter).
🧰 CSV Module in Python
Python provides a built-in module named csv to work with CSV files.
import csv
import csv
Function Description with open('data.csv', 'r') as file:
csv.reader() Reads CSV as list of rows reader = csv.reader(file)
csv.writer() Writes list of data to CSV for row in reader:
print(row)
csv.DictReader() Reads CSV as dictionaries
csv.DictWriter() Writes dictionaries to CSV import csv
with open('data.csv', 'w', newline='') as file:
writer = csv.writer(file)
import csv writer.writerow(['Name', 'Age’])
with open('data.csv', 'w', newline='') as file: writer.writerow(['Alice', 23])
fieldnames = ['Name', 'Age']
writer = csv.DictWriter(file, fieldnames=fieldnames) import csv
writer.writeheader() with open('data.csv', 'r') as file:
writer.writerow({'Name': 'Bob', 'Age': 30}) reader = csv.DictReader(file)
for row in reader:
print(row['Name'], row['Age'])
🔹 Opening a CSV File
Use the built-in open() function:
file = open('data.csv', 'r') # For reading

🔸 Reading a CSV File


Using csv.reader()
import csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
🔹 Opening a CSV File
Use the built-in open() function:
file = open('data.csv', 'r') # For reading

🔸 Reading a CSV File


Using csv.DictReader()

import csv
with open('data.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['Name'], row['Age'])
🔸 Writing to a CSV File
1. Using csv.writer()
import csv
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'City’]) # Writing header
writer.writerow(['Amit', 17, 'Delhi’]) # Writing row
🔸 Writing to a CSV File
2. Using csv.DictWriter()
import csv
with open('output.csv', 'w', newline='') as file:
fieldnames = ['Name', 'Age', 'City’]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Name': 'Riya', 'Age': 18, 'City': 'Mumbai'})

📝 Points to Remember
1.Always use newline='' while opening files for writing to avoid extra blank lines.
2.with open(...) is recommended as it automatically closes the file.
3.A CSV file can use different delimiters like comma ,, semicolon ;, etc.
Example
import csv
employees = [ ['EmpID', 'Name', 'Department', 'Salary’], # Header row
['E001', 'Rohan Kumar', 'HR', 45000],
['E002', 'Anita Sharma', 'Finance', 55000],
['E003', 'Sandeep Verma', 'IT', 60000],
['E004', 'Neha Singh', 'Marketing', 48000] ]

with open('employee.csv', 'w', newline='') as file:


writer = csv.writer(file)
writer.writerows(employees)

print("employee.csv file has been created successfully.")


✅ Python Program to Read employee.csv using csv.reader()
import csv
with open('employee.csv', 'r') as file:
reader = csv.reader(file)

print("Employee Details:\n")
for row in reader:
print(row)

🔄 Alternate: Reading with csv.DictReader() (for better readability)


with open('employee.csv', 'r') as file:
reader = csv.DictReader(file)

print("Employee Records:\n")
for row in reader:
print(f"ID: {row['EmpID']}, Name: {row['Name']},
Department: {row['Department']},
Salary: {row['Salary']}")
📚 Class XII CBSE Important Practice Questions
1.What is a CSV file? How is it different from a text file?
2.How do you read data from a CSV file using csv.reader()?
3.Write a Python program to write a list of student data to a CSV file.
4.Explain the role of DictReader and DictWriter.
5.Write a program to read only names from a CSV file.
PYTHON TRENDS
CLASS: XII (C.B.S.E.)

BY:
SANJEEV BHADAURIA
PGT CS (KV DUG)

You might also like