You are on page 1of 2

PRACTICAL-14

Ques. Write user-defined functions to perform read and write


operations onto a ‘student.csv’ file having fields like roll number ,
name, stream and marks .
import csv
row=['2','Akshat Chauhan','Commerce','98']
def readcsv():
f=open("student.csv",'r')
data =csv.reader(f)
for row in data :
print(row)
def writecsv():
with open("student.csv",'w')as fav:
csv_w=csv.writer(fav)
csv_w.writerow(row)
print("Press -1 to Read Data and Press-2 to Write Data :")
a=int(input())
if a==1:
readcsv()
elif a==2:
writecsv()
else :
print("Invalid Value")
OUTPUT
Press -1 to Read Data and Press-2 to Write Data :
1
['1', 'Akash', 'Commerce', '88']
['3', 'Ashi', 'Non-Medical', '90']
['4', 'Jeet', 'Medical', '85']

➢ Student.csv
1,Akash,Commerce,88
3,Ashi,Non-Medical,90
4,Jeet,Medical,85

You might also like