CSV FILE PROGRAMS
1. Write a program to store student data (Roll No, Name, Class) of 5 students in a
CSV file named students.csv.
2. Write a Python program to input item details (Item ID, Item Name, Price) and write
them to a file items.csv using the writerow() method.
3. Given a list of employees with (EmpID, Name, Salary), write a function to save all
employee details into a CSV file called employees.csv.
4. Accept movie details from the user (Movie ID, Title, Genre) and store them into
movies.csv file, with each movie on a new row.
5. Write a program to create a CSV file books.csv and write at least 4 book records
using a writer object.
6. Write a Python program to read and display all records from a file named
students.csv.
7. Read the file items.csv and display only those items whose price is greater
than 500.
8. Write a program to read data from employees.csv and display each employee's
name with their salary.
9. From a CSV file movies.csv, read all movies and count how many are of the
genre ‘Comedy’.
10. Write a function that reads a file books.csv and displays books whose name
starts with the letter ‘A’.
11. Read data from students.csv, add a new record, and save the updated content
into a new file updated_students.csv.
12. Write a Python program to copy contents from items.csv into
backup_items.csv.
13. Read all records from employees.csv, increase the salary by 10% for each
employee, and write the updated records to updated_employees.csv.
14. Read data from books.csv, and remove all books with price less than 100. Write
the filtered data to books_new.csv.
15. Read from movies.csv and create a new file short_movies.csv containing only
the movies whose title has less than 10 characters.
16. Given a code snippet that writes to a CSV file using csv.writer, predict the output
file content.
CSV File (student.csv)
RollNo,Name,Class
101,Amit,12
102,Neha,12
103,Rahul,12
Python Code:
import csv
with open("students.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(" | ".join(row))
17. Identify and correct errors in the following code snippet for reading data from a CSV file.
18. What will be the output of the following code if the file books.csv contains... (followed by
given content)?
books.csv
BookID,Title,Price
1,Python Basics,300
2,AI Guide,500
3,Algo,200
Code:
import csv
with open("books.csv", "r") as file:
reader = csv.reader(file)
next(reader)
for row in reader:
if int(row[0]) % 2 == 1:
print(row[1])
19. Fill in the blanks to complete a program that reads from books.csv and writes selected
records to filtered_books.csv.
20. Write a program that allows a user to enter multiple contact records (Name, Phone, Email)
and search for a record by name from the contacts.csv file.
21. Create a CSV file marks.csv with fields (Roll No, Name, Marks in 5 subjects), and
calculate the total and average marks for each student after reading.
22. Read from marks.csv, and write only those records to passed.csv where average is
more than 40.
23. Write a program that reads from contacts.csv and prints the total number of
contacts with Gmail IDs.