CSV Questions
CSV Questions
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",newline="")
h=['pid','pname','cost','qty']
r=csv.DictWriter(f1,fieldnames=h)
r.writeheader()
v=[{'pid':'p1','pname':'brush','cost':'50','qty':'200'},
{'pid':'p2','pname':'toothbrush','cost':'12','qty':'150'},
{'pid':'p3','pname':'comb','cost':'40','qty':'300'},
{'pid':'p5','pname':'pen','cost':'10','qty':'250'}]
r.writerows(v)
f.close()
#-------------------------------------------------------
'''
write a python function writecsv () to write the information into
product.csv. using dictionary. columns of product .csv is as follows:
pid,pname,cost,quantity
'''
#solution---------------------------------------------
def writecsv():
f=open("product.csv","w",newline="")
h=['pid','pname','cost','qty']
r=csv.DictWriter(f1,fieldnames=h)
r.writeheader()
while True:
i=int(input("enter id"))
n=input("enter product name")
c=int(input("enter cost"))
q=int(input("enter qty"))
v={'pid':i,'pname':n,'cost':c,'qty':q}
r.writerow(v)
ch=input("more records")
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
'''
import csv
def readcsv():
f=open("product.csv","r")
r=csv.DictReader(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.DictReader(f)
next(r)
for i in r:
if i['cost']>'150':
print(i)
#--------------------------------------------------------------------------
---
'''
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.DictReader(f)
n=input("enter product id which u want to search")
for i in r:
if i['pid']==n:
print(i)
#--------------------------------------------------------------------------
---
'''
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.DictReader(f)
for i in r:
if i['pname']=="pen":
print(i)
#--------------------------------------------------------------------------
---
'''
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.DictReader(f)
next(r)
c=0
for i in r:
c=c+1
print(c)
#--------------------------------------------------------------------------
---
'''
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.DictReader(f)
f=0
for i in r:
if (len(i['pname'])>6):
print(i)
f+=1
if(f==0):
print("record not found")
#--------------------------------------------------------------------------
'''
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.DictReader(f)
next(r)
m=-1
for i in r:
if (int(i['cost'])>m):
m=int(i['cost'])
d=i
print(d)
#--------------------------------------------------------------------------
-
'''
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.DictReader(f)
next(r)
s=0
for i in r:
s=s+int(i['cost'])
print("total cost is",s)
#--------------------------------------------------------------------------
---