CSV (Comma Separated Values) is a simple file format used to store data in tabular form such as excel sheet or database.
Each line of the file is a data record.
Each record consists of one or more fields, separated by commas. The csv module is used for working with CSV files in python.
It is used to read and write tabular data in CSV format.
There are 3 main modes in which the files can be opened: Read, Write and Append mode.
Read mode (denoted by ‘r’): It reads contents from a file. The file should exist else it gives an error.
Write mode (denoted by ‘w’): It writes to a CSV file. If the file exists, it clears it and starts writing to it from the first row. Else, if the file does not exist, it is created and then the data is written to it.
Append mode (denoted by ‘a’): It writes rows to a pre-existing file. It is different from write mode as it does not clear the existing rows of the CSV file, it just adds the rows below them.
Open notepad and type contents as
name,class, marks
ajay,12,75
vijay,11,85
then save the file named as"abc.csv"
# reading csv file contents
import csv
f=open("abc.csv",'r')
r=csv.reader(f) # canvert file object into reader object
for row in r:
print(row)
Every record is stored in reader object in form of List
write, append, serach, delete and display operations in CSV File
import csv
def Add():
f=open("abc.csv","w")
r=csv.writer(f)
name=input("Enter name")
rollno=int(input("Enter rollno "))
marks=int(input("Enter marks"))
cls=int(input("Enter class"))
l=[name,rollno,marks,cls]
r.writerow(l)
f.close()
def Append():
f=open("abc.csv","a")
r=csv.writer(f)
name=input("Enter name")
rollno=int(input("Enter rollno "))
marks=int(input("Enter marks"))
cls=int(input("Enter class"))
l=[name,rollno,marks,cls]
r.writerow(l)
f.close()
#Search record rollno wise
def search():
f=open("abc.csv","r",newline="\n")
r=csv.reader(f)
for i in r:
if(int(i[1]))==2:
print(i)
f.close()
#delete record for given rollno
def delete():
rollno=int(input("enter rollno for deletion"))
f=open("abc.csv","r",newline="\n")
r=csv.reader(f)
l=[]
for i in r:
if(int(i[1]))!=rollno:
l.append(i)
#print(i,"Record deleted")
f.close()
f=open("abc.csv","w")
wr=csv.writer(f)
wr.writerow(l)
f.close()
#display all records of abc.csv file
def disp():
f=open("abc.csv","r",newline="\n")
r=csv.reader(f)
print(r)
for i in r:
print(i)
f.close()
#Add()
#Append()
#Append()
#disp()
#search()
#delete()
#disp()
join() is string method that join all values of each row with comma separator
Assignments:
Which function is used to read data from CSV file ? [ reader() ]
What will be the extension of Python script file ? [.py]
What will be the extension of csv file ? [.csv]
Which module required to import for reading csv files ? [csv]
Expand: CSV
Comma Separated Value
Which of the following module is required to import to work with CSV file?
File csv pandas numpy
Which of the following is not a function of csv module?
readline()
writerow()
reader()
writer()
Writing method for csv files ?
Q. Name Writing method in csv file:
1. csv.writer() – returns a writer object which writes data into csv file.
syntax: writerobject= csv.writer(fileobject)
example : stuwriter=csv.writer(fh)
2. writerobject.writerow() – writes one row of data onto the writer object
example : stuwriter.writerow(sturec) #sturec=[23,’Rustom’,79]
3. writerobject.writerows()- writes multiple rows of data onto the writer
example : stuwriter.writerows(sturec) # sturec=[[11,’Nishtha’,99],[12,’Rudy’,’89.0]]
Q. Steps to Write data in csv file:
* Import csv module
* Open csv file in a file handle( just as text file)
Example : fh=open(“stu.csv”,”w”)- to open the file in write mode
* Create a writer object by using csv.writer() function.
Ex. stuwriter=csv.writer(fh)
* Obtain user data and form a Python sequence(list or tuple) out of it.
Ex. sturec=(11, ‘Neelam’,79.0)
* Write the Python sequence onto the writer object using csv.writerow() or csv.writerows()
Ex. stuwirter.writerow(sturec)
Q. Write Steps to read data from a csv file:
* Import csv module
* Open csv file in a file handle( just as text file)
Example: fh=open(“stu.csv”,”r”)- to open the file in read mode
* Create a reader object by using csv.reader() function
Example: stureader=csv.reader(fh)
* using for loop fetch the data from the reader object in which data is stored in the form of iterable