CSV Practice
CSV Practice
1. Define Queue.
Answer:
Queue is a first-in, first-out (FIFO) data structure, i.e. the element added first to the queue
will be the one to be removed first. Elements are always added to the rear of the queue
and removed from the front of the queue.
1.Traversal
2.Insertion
3. Deletion
4. Searching
1. Start
2. Check FRONT and REAR value,
1. if both the values are -1, then FRONT and REAR are incremented
by 1
2. other wise Rear is incremented by one.
3. queue [Rear]=new element.
4. Stop
Question 4:
Write an algorithm to implement deletion operation of queue.
Answer:
1. Start
2. Check for underflow situation by checking value of Front=-1
1. If it is display appropriate message and stop
2. Otherwise Deleted item=queue [Front]
3. If Front=Rear then Front=Rear=-1 Otherwise Front is incremented by one
4. Print “Item Deleted”
5. Stop
Question 1:
Write Insert (city) and Delete (City) methods in Python to add City and REmOve City
considering them to act as Insert and Delete operations of the data structure Queue.
Answer:
city=[ ]
def Insert (city):
a = raw_input(“Enter city”)
city. append (a)
def Delete (city):
if (city = = []):
print “Queue empty”
else:
print ( “Deleted element is”,city.pop[0])
Question 5:
Write Insert (Place) and Delete (Place) methods in Python to be add Place and Remove Place
considering them to act as Insert and Delete operations of the data structure Queue.
Answer:
place=[]
def Insert (place):
a=raw_input (“Enter city”)
place. append (a)
def delete (place):
if (place = = []):
print “Queue empty”
else:
print (“Deleted element is”, place.pop [0])
Question 6:
TRY THIS:
1. Write Add(Fruit) and Remove (Fruit) methods in Python to insert name of a
Fruit and to delete name of a Fruit considering them to act as Insert and Delete
operations of the data structure Queue.
2. Write the deletion and display operation of queue containing characters IN A
LIST.
3. Write the deletion and display operation of queue containing numbers.
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)
2. 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
import csv
f = open("data.csv",'r')
d=csv.reader(f)
next(f) #To Skip Header Row
OUTPUT :
Enter admission number1231
Adm no = 1231
Name = Amit
Class = XII
Section = A
Marks = 45
3.Write a program to add/insert records in
file “data.csv”. Structure of a record is roll
number, name and class.
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()
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
-------------------------
d=csv.reader(f)
next(f)
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
--------------------------------
7.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
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( )
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()
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()
X
STD 11TH 12TH THEOR TOTAL
MAR MAR MAR Y PRACTICAL/ MARK
K K K TOTAL INTERNAL S