CSV File
CSV File
CSV File
Text files contain text which can be opened by any text editor and
there is plain text with no format
CSV also contain text data but in a format where each line is
considered as row/record which has many fields(columns).
fields are the values separated by a delimiter like , " ' ,"*","/" ,”\n”etc.
the first record is the title of each field.
To Read a CSV file
f=open("book1.csv“,”r”) # open an existing csv file Open a CSV file in read mode
for i in r:
Fetch data through loop i.e. row by row
print(i)
f.close() Close the file
Example -To read a csv file
f=open("book1.csv“,”r”, ) # open an existing csv file Open a CSV file in read mode
for i in r:
Fetch data through loop i.e. row by row
print(i)
f.close() Close the file
newline=“ “ (null string no space in between) with file open will ensure that no
translation of EOL character takes place
Example -To display a csv file in tabular format
import csv
f=open(r"C:\Users\user\Desktop\Book1.csv","r")
ro=csv.reader(f)
for i in ro:
print(i[0],"\t",i[1],"\t",i[2],"\t",i[3])
Example -To display a csv file in tabular format
or
create table from csv
def writecsv():
f=open("counties.csv","w")
r=csv.writer(f,lineterminator='\n')
r.writerow(['country','capital','code'])
r.writerow(['india','newdelhi','ii'])
r.writerow(['us','washington','uu'])
r.writerow(['malysia','kualaumpur','mm']) Calling of functions
r.writerow(['france','paris','ff']) writecsv()
searchcsv()
def searchcsv():
f=open("counties.csv","r")
r=csv.reader(f)
f=0
for i in r:
if (len(i[1])>6):
print(i[0])
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.
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
m=-1
for i in r:
if (int(i[2])>m): next()-
m=int(i[2])
d=i
print(d)
write a python function to search and display the total cost
of all products from the file PRODUCT.CSV.
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','120','150'])
r.writerow(['p3','comb','40','300']) Calling of functions
r.writerow(['p5','pen','10','250']) writecsv()
searchcsv()
def searchcsv():
f=open("product.csv","r")
r=csv.reader(f)
next(r)
s=0
for i in r:
s=s+int(i[2]) next()-
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.
def writecsv():
f=open("product.csv","w")
r=csv.writer(f,lineterminator='\n') def readcsv():
r.writerow(['pid','pname','cost','qty']) f=open("pro1.csv","r")
r=csv.reader(f)
r.writerow(['p1','brush','50','200'])
for i in r:
r.writerow(['p2','toothbrush','120','150']) print(i)
r.writerow(['p3','comb','40','300'])
r.writerow(['p5','pen','10','250'])
def searchcsv():
f=open("product.csv","r")
f1=open("pro1.csv","w") Calling of functions
r=csv.reader(f) writecsv()
w=csv.writer(f1,lineterminator='\n') searchcsv()
g=next(r) readcsv()
w.writerow(g)
for i in r:
if i[3]>'150': next()-
w.writerow(i)