4.3 Notes of CSV file
4.3 Notes of CSV file
4.3 Notes of CSV file
CSV files are delimited files that store tabular data where comma delimits every value.
The default delimiter of CSV files is the comma (,) but we can use different delimiter character other than
comma.
It preferred export and import format for databases and spread sheets.
It provides two special objects writer and reader to write and read in CSV files.
Use of newline: It is an additional argument but it is optional. It specifies that how would python
handle newline characters while working with CSV files.
1
Working on EOL (End of Line) character is depend on operating system because different operating system
uses different default EOL character.
Ex: f = open('sunshine.csv','w',newline='')
Statement
statement
Ex: f.close()
Python CSV module provides three functions first function to create writer object and other two functions
for writing data into the file.
1. writer(): It returns a writer object which writes data into CSV file.
Ex: wo = csv.writer(f)
Ex: wo.writerow([value1,value2,...])
Note: If we have all the data available and data is not much lengthy then it is possible to write all data in
one go by using writerows() function. But we need to create a nested sequence of data then we can write.
2
Note: It is important to convert the received data into the form appropriate for the CSV file. This task is
performed by writer object.
Note: We can use list data type or tuple data type to write data in the CSV file.
WAP to create a csv file data.csv in which write the record of 4 students like rno,name,marks.
import csv
f=open('data.csv','a')
wo=csv.writer(f)
wo.writerow(['Roll No','Name','Marks'])
for i in range(4):
wo.writerow([r,n,m])
f.close()
import csv
f=open('data.csv','a')
wo=csv.writer(f)
for i in range(2):
3
m=float(input('Enter marks :'))
wo.writerow([r,n,m])
f.close()
reader(): It returns a reader object which loads data from CSV file into an iterable after parsing
delimited data.
Note: The csv.reader() object does the opposite of csv.writer() object. The csv.reader() object loads data
from the CSV file, parse it and returns the data in form of a python iterable where we can fetch one by one
row by using for loop.
print(variable)
print(L)
Note: All data present in csv file is consider as string data type. It means file contains list of strings.
WAP to read and display record of all the students present in the file data.csv.
import csv
4
f=open('data.csv','r')
ro=csv.reader(f)
print(rec)
f.close()
Output:
[]
[]
[]
[]
[]
[]
[]
Note: We never entered empty rows. Then where have these empty rows come from? Because we did
not specify the newline argument in the file open() function while creating this CSV file. EOL translation
took place, which result in the blank rows after every record while reading.
Note: Every data record line was appended with EOL character '\r\n' on the windows.
(i) Method1: Write onto CSV file with newline=''(Without space) argument in open() function.
Ex: f = open('FileName.csv','w',newline='')
5
(ii) Method2: To read data from CSV file, which is created with EOL translation. Open it with
newline='\r\n' argument.
Ex: f = open('FileName.csv','r',newline='\r\n')
WAP to read and display record of all the students present in the file data.csv.
import csv
f=open('data.csv','r',newline='\r\n')
ro=csv.reader(f)
print(rec)
f.close()
Output: