CSV File Handling
CSV File Handling
A comma-separated values file is a text file that uses a comma to separate values. Each line
of the file is a data record. Each record consists of one or more fields, separated by
commas. The use of the comma as a field separator is the source of the name for this file
format.
Advantages:
CSV file stores data as text and can handle large amount of data.
Data is stored in a tabular form (rows and columns).
It is easy to export from data base and spread sheet software like Excel. It is easy to
import data from a csv file to a data base and spread sheet software.
CSV files are supported by nearly all data upload interfaces. If you are planning to
move your data between platforms, export and import it from one interface to another,
you might be better off with the CSV file format.
In CSV file, text data is in a format where each line is considered as a row/record with many fields.
Fields are separated by a delimiter like , or / or – etc.
The first line generally contains the header of the fields
DATA IN EXCEL FILE or DATA IN A CSV FILE
DATABASE TABLE
Writing in CSV files
write_data() write_data()
Reading CSV files
f.close()
def display():
f=open("country.csv","r")
rec=csv.reader(f)
print("Names of countries with more than 6 characters in
capital name")
next(rec)
for i in rec:
if len(i[1])>6:
print(i[0])
f.close()
write_data()
display()
A CSV file “product.csv” contains
<product id>,<product name>,<cost>
f.close()
Continued
#approach-2
def display_2():
f=open("product.csv","r")
rec=csv.reader(f)
print("Details of products with maximum cost")
next(rec)
max=0
for i in rec:
if int(i[2])>max:
max=int(i[2])
f.seek(0)
next(rec)
for i in rec:
if int(i[2])==max:
print(i)
f.close()
write_data()
display_1()
display_2()
A CSV file “product.csv” contains
<product id>,<product name>,<cost>
Write a function to append record of type product to the csv file
def append_data():
f=open("product.csv","a")
w=csv.writer(f,delimiter=',',lineterminator='\n')
id=input("Enter product id: ")
nm=input("Enter product name: ")
cost=float(input("Enter cost of product: "))
data=[id,nm,cost]
w.writerow(data)
f.close()
A CSV file “product.csv” contains
<product id>,<product name>,<cost>
Write a function to remove records on the basis of product name from the
csv file
def remove_data():
f=open("product.csv","r")
g=open("temp.csv","w")
w=csv.writer(g,delimiter=',',lineterminator='\n')
pn=input("Enter product name to be deleted for: ")
rec=csv.reader(f)
header=next(rec)
w.writerow(header)
for i in rec:
if i[1]!=pn:
w.writerow(i)
f.close()
g.close()
os.remove("product.csv")
os.rename("temp.csv","product.csv")
A CSV file “product.csv” contains
<product id>,<product name>,<cost>
Write a function to update cost of product on the basis of product name
def update_data():
f=open("product.csv","r")
g=open("temp.csv","w")
w=csv.writer(g,delimiter=',',lineterminator='\n')
pn=input("Enter product name to be updated: ")
rec=csv.reader(f)
header=next(rec)
w.writerow(header)
for i in rec:
if i[1]==pn:
i[2]=float(input("Enter updated cost: "))
w.writerow(i)
f.close()
g.close()
os.remove("product.csv")
os.rename("temp.csv","product.csv")
import csv MENU DRIVEN PROGRAM def display_data():
import os f=open("product.csv","r")
rec=csv.reader(f)
def write_data(): print("Data in the File")
f=open("product.csv","w") for i in rec:
w=csv.writer(f,delimiter=',',lineterminator='\n') print(i)
w.writerow(["Product Id", "Product name","Cost"])
w.writerow(["P1","TV", 20000] ) def search_data():
w.writerow(["P2","LAPTOP", 50000] ) f=open("product.csv","r")
w.writerow(["P3","WASHING MACHINE", 50000] ) rec=csv.reader(f)
flag=0
f.close() pn=input("Enter product name to be searched for: ")
for i in rec:
if i[1]==pn:
def append_data(): print(i)
f=open("product.csv","a") flag=1
w=csv.writer(f,delimiter=',',lineterminator='\n')
id=input("Enter product id: ") if flag==0:
nm=input("Enter product name: ") print("Unsuccessful search")
cost=float(input("Enter cost of product: ")) f.close()
data=[id,nm,cost]
w.writerow(data)
f.close()
MENU DRIVEN PROGRAM (Continued)
write_data()
while(True):
print("Enter 1 to append, 2 to read, 3 to search, 4 to delete, 5 to update")
choice=int(input("Enter choice: "))
if choice==1:
append_data()
elif choice==2:
display_data()
elif choice==3:
search_data()
elif choice==4:
remove_data()
elif choice==5:
update_data()
else:
break
FOR FILE