CSV Programs
CSV Programs
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
#solution---------------------------------------------
def writecsv():
f=open("product.csv","w")
r=csv.writer(f,lineterminator='\n')
r.writerow(['pid','pname','cost','qty'])
r.writerow(['p1','brush','50','200'])
r.writerow(['p2','toothbrush','12','150'])
r.writerow(['p3','comb','40','300'])
r.writerow(['p5','pen','10','250'])
f.close()
#------------------------------------------------------------------------
'''
write a python function writecsv () to write the following information into
product.csv.Heading of the product .csv is as follows
pid,pname,cost,quantity
'''
f=open("marks.csv","w")
r=csv.writer(f,newline="")
r.writerow(['sno','name','marks'])
while True:
n=input("enter your name")
m=int(input("enter marks"))
s=int(input("enter rollno"))
v=[s,n,m]
r.writerow(v)
ch=input("more records(y/n)")
if(ch=='n'):
break
f.close()
#------------------------------------------------------------------------
'''
write a python function readcsv () to display the following information into
product.csv. assume that following info is already present in the file.
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
import csv
def readcsv():
f=open("product.csv","r")
r=csv.reader(f)
for i in r:
print(i)
f.close()
#-------------------------------------------------------------------------
'''
write a python function searchcsv()to find only those records from the file
product.csv whose quantity is more than 150. Also include the first row with
headings sample of product.csv is given below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
for i in r:
if i[3]>'150':
print(i)
f.close()
#-----------------------------------------------------------------------------
'''
write a python function searchcsv() to find only those records from the file
product.csv whose product id entered by the user.also include the first row with
headings sample of product.csv is given below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
n=input("enter product id which u want to search")
for i in r:
if i[0]==n:
print(i)
f.close()
#-----------------------------------------------------------------------------
'''
write a python function searchcsv() to display "pen" record information from the file
product.csv also include the first row with headings sample of product.csv is given
below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
for i in r:
if i[0]=="pen":
print(i)
f.close()
#-----------------------------------------------------------------------------
'''
write a python function searchcsv() to count total no of records from product.csv is
given below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
c=0
for i in r:
c=c+1
print(c)
f.close()
#-----------------------------------------------------------------------------
'''
A csv file product.csv contains data in the following order:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
write a python function searchcsv() to read the file product.csv and display
the names of all those product whose no of characters in the pname are
more than 6.
'''
import csv
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
f=0
for i in r:
if (len(i[1])>6):
print(i[1])
f+=1
if(f==0):
print("record not found")
f.close()
#--------------------------------------------------------------------------
'''
write a python function to search and display the record of that product from the file
PRODUCT.CSV which has maximum cost.
sample of product.csv is given below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
m=-1
for i in r:
if (int(i[2])>m):
m=int(i[2])
d=i
print(d)
f.close()
#---------------------------------------------------------------------------
'''
write a python function to search and display the total cost of all products
from the file PRODUCT.CSV.
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
s=0
for i in r:
s=s+int(i[2])
print("total cost is",s)
#-----------------------------------------------------------------------------
'''
write a python function to find transfer
only those records from the file product.csv to another file "pro1.csv"
whose quantity is more than 150. also include the first row with headings
sample of product.csv is given below:
pid,pname,cost,quantity
p1,brush,50,200
p2,toothbrush,120,150
p3,comb,40,300
p4,sheets,100,500
p5,pen,10,250
'''
def searchcsv():
f=open("product.csv","r")
f1=open("pro1.csv","w")
r=csv.reader(f)
w=csv.writer(f1,lineterminator='\n')
g=next(r)
w.writerow(g)
for i in r:
if i[3]>'150':
w.writerow(i)
f.close()