CSV_FILE_PROGRAM
CSV_FILE_PROGRAM
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()