Important Questions of CSV File in Python
Q1. Write a program to read entire data from file data.csv
Ans.
import csv
f=open("data.csv", 'r')
d=csv.reader(f)
for row in d:
print(row)
OUTPUT:
['Admno', 'Name', 'Class', 'Sec', 'Marks']
['1231', 'Amit', 'XII', 'A', '45']
['1224', 'Anil', 'XII', 'B', '49']
['1765', 'Suman', 'XI', 'A', '42']
['2132', 'Naman', 'XII', 'C', '38']
OR
We can also write the program in the following way
import csv
with open("data.csv",'r') as f:
d=csv.reader(f)
for row in d:
print(row)
Important Questions of CSV File in Python
Q2. Write a program to search the record from “data.csv” according to the admission number input from the user. Structure of record saved in “data.csv” is Adm_no, Name, Class, Section, Marks
Ans.
import csv
f = open("data.csv",'r')
d=csv.reader(f)
next(f) #To Skip Header Row
k = 0
adm = int(input("Enter admission number"))
for row in d:
if int(row[0])==adm:
print("Adm no = ", row[0])
print("Name = ", row[1])
print("Class = ", row[2])
print("Section = ", row[3])
print("Marks = ", row[4])
break
else:
print("Record Not Found")
OUTPUT :
Enter admission number1231
Adm no = 1231
Name = Amit
Class = XII
Section = A
Marks = 45
Important Questions of CSV File in Python
Q3. Write a program to add/insert records in file “data.csv”. Structure of a record is roll number, name and class.
Ans.
import csv
field = ["Roll no" , "Name" , "Class"]
f = open("data.csv" , 'w')
d=csv.writer(f)
d.writerow(field)
ch='y'
while ch=='y' or ch=='Y':
rn=int(input("Enter Roll number: "))
nm = input("Enter name: ")
cls = input("Enter Class: ")
rec=[rn,nm,cls]
d.writerow(rec)
ch=input("Enter more record??(Y/N)")
f.close()
Important Questions of CSV File in Python
Q4. Write a program to copy the data from “data.csv” to “temp.csv”
Ans.
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( )
Important Questions of CSV File in Python
Q5. Write a program to read all content of “student.csv” and display records of only those students who scored more than 80 marks. Records stored in students is in format : Rollno, Name, Marks
Ans.
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
-------------------------
Important Questions of CSV File in Python
Q6. Write a program to display all the records from product.csv whose price is more than 300. Format of record stored in product.csv is product id, product name, price,.
Ans.
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("Product id =", i[0])
print("Product Name =", i[1])
print("Product Price =", i[2])
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
--------------------------------
Important Questions of CSV File in Python
Q7. Write a program to calculate the sum of all the marks given in the file “marks.csv. Records in “marks.csv” are as follows :
Rollno, Name, Marks
1, Suman, 67
2, Aman,71
3, Mini, 68
4, Amit, 80
Ans.
import csv
f=open("marks.csv","r")
d=csv.reader(f)
next(f)
s=0
for i in d:
s=s + int(i[2])
print("Total Marks are " ,s)
f.close( )
Important Questions of CSV File in Python
Q8. Write a program to count number of records present in “data.csv” file.
Ans.
import csv
f = open("data.csv" , "r")
d = csv.reader(f)
next(f) #to skip header row
r = 0
for row in d:
r = r+1
print("Number of records are " , r)
Important Questions of CSV File in Python
Q9. Write a program to modify the record of a student in a file “data.csv”. Following records are saved in file.
Rollno, Name, Marks
1, Aman, 35
2, Kanak, 1
3, Anuj, 33
4, suman, 25
Ans.
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
rollno = int(input("Enter roll number :"))
mn=input("Enter modified name :")
mm = int(input("Enter modified marks :"))
mr=[rollno,mn,mm]
header =["Rollno", "Name", "Marks"]
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
After modification, data in the file will be
Rollno, Name, Marks
1, Aman, 35
2, Kanak, 1
3, Anuj, 33
4, sumati, 45
Important Questions of CSV File in Python
Q10. Write a program to show the detail of the student who scored the highest marks. Data stored in “Data.csv” is given below :
Rollno, Name, Marks
1, Aman, 35
2, Kanak, 1
3, Anuj, 33
4, suman, 25
Ans.
import csv
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 :
['1' , ' Aman' , ' 35']
Related Post
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series
Like this:
Like Loading...
Related