PROGRAM-12
READ AND WRITE OPERATION IN CSV FILES
OBJECTIVE:
To write user defined functions to perform read and write operation onto a ‘student.csv’ file
having fields as roll_no, name and marks.
INPUT:
Read data from user
OUTPUT:
Display content of the file
CODING:
import csv
def WRITE():
f = open(“student.csv”, “w”, newline = “ ”)
csv_writer = csv.writer(f)
csv_writer.writerow([“Roll_no”, “Name”, “Marks”])
ans = “y”
while ans == “y”:
Rno = int(input(“Enter the roll number”))
name = input(“Enter the name”)
marks = int(input(“Enter the marks”))
sturec = [Rno, name, marks]
csv_writer.writerow(sturec)
ans = input(“Enter y to continue or n to exit”)
f.close()
def READ():
f1 = open(“student.csv”, “r”, newline = “ ”)
csv_read = csv.reader(f1)
for line in csv_read:
print(line)
f1.close()
RESULT:
The program was executed and output was obtained successfully.