0% found this document useful (0 votes)
5 views

CSV_FILE_PROGRAM

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CSV_FILE_PROGRAM

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a program to create a CSV file “Student.

csv” and insert the following data into the file


and perform the following task: [10]

Roll Name Marks Grade


1 Aman 98 A
2 Shreya 87 B
3 Rahul 67 D
4 Rajesh 92 A

(i) Display the Name of those students whose grade is A.


(ii) Display the entire record of those students whose name is starting with ‘R” and
ending with ‘h’.
(iii) Decrease the marks of “Aman” by 10% in the file.
(iv) Delete those records of students from the file whose marks is more than 70.

Solution:
import csv

def write():
f=open("E:\Student1.csv","w",newline='')
w=csv.writer(f)
w.writerow(['Roll','Name','Marks','Grade'])
for i in range(4):
roll=int(input("Enter roll no."))
name=input("Enter name")
marks=float(input("Enter marks"))
Grade=input("Enter grade")
l=[roll,name,marks,Grade]
w.writerow(l)
f.close()

def display():
f=open("E:\Student1.csv","r")
print("The content of the file:")
f.seek(0)
csv_f=list(csv.reader(f))
for i in csv_f:
print(*i)
f.close()

def display1():
f=open("E:\Student1.csv",'r')
f.seek(0)
csv_f=list(csv.reader(f))
print("The content of the file whose grade is A:")
for i in csv_f:
if i[3]=="A":
print(*i)
f.close()

def display2():
f=open("E:\Student1.csv","r")
csv_f=list(csv.reader(f))
f.seek(0)
print("The content of the file whose name starting with R and ending with h:")
for i in csv_f:
if i[1][0]=="R" and i[1][-1]=="h":
print(*i)
f.close()

def update():
f=open("E:\Student1.csv","r")
csv_f=list(csv.reader(f))
f=open("E:\Student1.csv","w",newline='')
w=csv.writer(f)
f.seek(0)
for i in csv_f:
if i[1]=="Rajesh":
i[2]=str(float(i[2])*0.9)
w.writerow(i)
else:
w.writerow(i)
f.close()

def delete():
f=open("E:\Student1.csv","r")
csv_f=list(csv.reader(f))
f.close()
f=open("E:\Student1.csv","w",newline='')
f.seek(0)
w=csv.writer(f)
for i in csv_f:
if i[2]>str(float(70.0)):
continue
else:
w.writerow(i)
f.close()

write()
display()
display1()
display2()
update()
display()
delete()
display()

You might also like