Created on Aug 4 2025
"""
#CSV FILES
# write a record with student roll no, Name and marks
#in the csv file using writerow( ) function
'''
import csv
fh=open("student.csv","w",newline="")
stuwriter=csv.writer(fh) # stuwriter is the object of the csv.writer( ) func
stuwriter.writerow(['ROLLNO','NAME','MARKS'])
rec=[]
while True:
print("Enter the student's record ")
r=int(input("Enter the rollno : "))
n=input("Enter the name : ")
m=int(input("Enter the marks : "))
sturec=[r,n,m]
rec.append(sturec)
ch=input(" Do you want to enter more records ? (Y/N) ")
if ch in "Nn":
break
for i in rec: # to write records one by one
stuwriter.writerow(i)
fh.close()
#Reading from the csv file
fh=open("student.csv","r")
stureader=csv.reader(fh) # stureader is the object of the csv.reader( ) func
for i in stureader: # to read records one by one
print(i)
fh.close()
'''
#WAP to write, read and search data from a CSV file
#csv file should contain itemno,itemname,quantity and price
import csv
def write():
f=open("items.csv","w",newline="")
swriter=csv.writer(f)
swriter.writerow(["INo","IName","Qty","Price"])
rec=[]
while True:
n=int(input("Item No : "))
name=input("Enter Item Name : ")
qty=int(input("Enter Quantity : "))
price=int(input("Enter Price : "))
data=[n,name,qty,price]
rec.append(data)
ch=input(" Do you want to enter more records ? (Y/N) ")
if ch in "Nn":
break
swriter.writerows(rec)
print("Data written in csv file successfully : ")
f.close()
def read():
f=open("items.csv","r")
sreader=csv.reader(f)
for i in sreader:
print(i)
f.close()
def searchitmno():
f=open("items.csv","r")
print("Searching data from a csv file : ")
s=input("Enter the Item No to be searched : ")
found=0
sreader=csv.reader(f)
for i in sreader:
if i[0]==s:
print(i)
found=1
break
if found==0:
print("Sorry Record Not Found : ")
f.close()
def searchitmname():
f=open("items.csv","r")
print("Searching data from a csv file : ")
s=input("Enter the Item Name to be searched : ")
found=0
sreader=csv.reader(f)
for i in sreader:
if i[1]==s:
print(i)
found=1
break
if found==0:
print("Sorry Record Not Found : ")
f.close()
#write()
#read()
#searchitmno()
#searchitmname()
Write a Program in Python that defines and calls the following user defined functions:
(i) add() – To accept and add data of EMPLOYEE to a CSV file ‘emp.csv’. Each record consists
of a list with field elements as empid, ename and esalary to store employee id, employee
name and employee salary respec- tively.
(ii) search()- To display the records of the employee whose salary is more than 10000.
(iii) display( ) – To display all the records that have salary less than 3000
def display():
with open(“worker.csv”, “r”) as csvfile:
csvreader = csv.reader(csvfile)
for rows in csvreader:
if rows[2]<3000:
print(“workder id=”,rows[0])
print(“name=”,rows[1])
(i)def add():
with open(“emp.csv”, “a”, newline=’’) as fhcsv:
csv_writer=csv.writer(fhcsv)
r=int(input(“enter empid.csv :”))
n=input(“enter name :”)
m=int(input(“enter salary :”))
rec=[r,n,m]
csv_writer.writerow(rec)
(ii)def search():
with open(“emp.csv”, “r”) as csvfile:
csvreader = csv.reader(csvfile)
for rows in csvreader:
if rows[2]>10000:
print(rows)