0% found this document useful (0 votes)
2 views4 pages

CSV file handling

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Class XII Computer Science

Quick Notes on Chapter: CSV File Handling

1. What is a CSV File?

● CSV stands for Comma-Separated Values.


● Used to store tabular data (e.g., spreadsheets, databases).
● Each line represents a record, with fields separated by commas.
● Commonly used in programs like Excel and OpenOffice Calc.

2. Characteristics of a CSV File

● A plain text file format.


● Compatible with various software for import/export.
● Example structure:

Name, Age, City


John, 25, New York
Alice, 30, Los Angeles

3. CSV Module in Python

● Python provides the csv module to handle CSV files.


● Steps to work with CSV files:
○ Import the csv module.
○ Open the file using open().
○ Use appropriate reader or writer objects.

4. Reading from a CSV File

● Use csv.reader(file_object, delimiter=',').


● Steps:
1. Import csv.
2. Open the CSV file in read mode ('r') using with.
3. Create a reader object to parse data.
4. Iterate through rows to access values.
● Example code:

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

5. Writing to a CSV File

● Use csv.writer(file_object, delimiter=',').


● Two methods:
1. writerow(): Writes one row at a time.
2. writerows(): Writes multiple rows at once.
● Steps:
1. Import csv.
2. Open the CSV file in write ('w') or append ('a') mode.
3. Create a writer object.
4. Use writerow() or writerows() to add data.

Example code:

import csv
with open('data.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'City'])
writer.writerow(['John', 25, 'New York'])

6. Creating a CSV File

● Method 1: Using Excel


○ Enter data in rows and columns.
○ Save the file with .csv extension.
● Method 2: Using Notepad
○ Type data as comma-separated values.
○ Save with .csv extension (enclose name in quotes, e.g., "data.csv").

7. Common Operations

● Count Records:
○ Iterate through rows and count.
● Search Data:
○ Compare field values while reading rows.
● Perform Calculations:
○ Sum specific fields like salary.

8. Advantages of with Block

● Automatically closes the file after operations.


● Prevents errors due to unclosed files.

9. Key Points

● Use the appropriate delimiter if the CSV uses characters other than commas.
● Handle exceptions for missing files or incorrect formats.

10. Example Program

● Program to Create and Search Records:

import csv

# Writing data to CSV

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

writer = csv.writer(file)

writer.writerow(['EmpNo', 'Name', 'Salary'])

while True:

empno = input("Enter Employee Number: ")

name = input("Enter Employee Name: ")

salary = input("Enter Employee Salary: ")

writer.writerow([empno, name, salary])

if input("Add More? (y/n): ").lower() != 'y':

break

# Searching data in CSV

with open('employees.csv', 'r') as file:

reader = csv.reader(file)

data = list(reader)

empno_to_search = input("Enter Employee Number to Search: ")

for row in data[1:]:


if row[0] == empno_to_search:

print(f"Name: {row[1]}, Salary: {row[2]}")

break

else:

print("Employee Not Found")

You might also like