Important Questions of CSV File in Python
Important Questions of CSV File in Python
import csv
f=open("data.csv", 'r')
d=csv.reader(f)
for row in d:
print(row)
OUTPUT:
OR
import csv
with open("data.csv",'r') as f:
d=csv.reader(f)
for row in d:
print(row)
f = open("data.csv",'r')
d=csv.reader(f)
k=0
for row in d:
if int(row[0])==adm:
break
else:
OUTPUT :
Enter admission number1231
Adm no = 1231
Name = Amit
Class = XII
Section = A
Marks = 45
import csv
f = open("data.csv" , 'w')
d=csv.writer(f)
d.writerow(field)
ch='y'
rec=[rn,nm,cls]
d.writerow(rec)
f.close()
import csv
f=open("data.csv","r")
f1=open("temp.csv",'w')
d=csv.reader(f)
d1=csv.writer(f1)
for i in d:
d1.writerow(i)
f.close( )
f1.close( )
import csv
f=open("student.csv","r")
d=csv.reader(f)
next(f)
print("Students Scored More than 80")
print()
for i in d:
if int(i[2])>80:
print("Roll Number =", i[0])
print("Name =", i[1])
print("Marks =", i[2])
print("--------------------")
f.close( )
OUTPUT
Students Scored More than 80
Roll Number = 1
Name = Amit
Marks = 81
--------------------------
Roll Number = 2
Name = Suman
Marks = 85
-------------------------
Roll Number = 4
Name = Deepika
Marks = 89
-------------------------
import csv
f=open("product.csv" , "r")
d=csv.reader(f)
next(f)
print("product whose price more than 300")
print()
for i in d:
if int(i[2])>300:
print("--------------------")
f.close( )
OUTPUT:
product whose price more than 300
Product id =2
Product Name = Mouse
Product Price = 850
---------------------------------
Product id =3
Product Name = RAM
Product Price = 1560
--------------------------------
import csv
f=open("marks.csv","r")
d=csv.reader(f)
next(f)
s=0
for i in d:
s=s + int(i[2])
f.close( )
import csv
f = open("data.csv" , "r")
d = csv.reader(f)
r=0
for row in d:
r = r+1
import csv
import os
f=open("data.csv","r")
f1 = open("temp.csv","w")
d=csv.reader(f)
d1=csv.writer(f1)
next(f)
s=0
mr=[rollno,mn,mm]
d1.writerow(header)
for i in d:
if int(i[0])==rollno:
d1.writerow(mr)
else:
d1.writerow(i)
os.remove("data.csv")
os.rename("temp.csv","data.csv")
f.close()
f1.close()
Execution of Program
Enter roll number :4
Enter modified name :Sumati
Enter modified marks :45
f=open("data.csv","r")
d=csv.reader(f)
next(f)
max=0
for i in d:
if int(i[2])>max:
max=int(i[2])
f.close()
f=open("data.csv","r")
d=csv.reader(f)
next(f)
for i in d:
if int(i[2])==max:
print(i)
f.close()
Execution of Program :