CSVFILES
CSVFILES
CSVFILES
CSV files are commonly used because they are easy to read and manage, small in size,
and fast to process/transfer. Because of these salient features, they are frequently used
in software applications, ranging anywhere from online e-commerce stores to mobile
apps to desktop tools. For example, Magento, an e-commerce platform, is known for its
support of CSV.
Thus, in a nutshell, the several advantages that are offered by CSV files are as follows:
• CSV is faster to handle.
• CSV is smaller in size.
• CSV is easy to generate and import onto a spreadsheet or database.
• CSV is human readable and easy to edit manually.
• CSV is simple to implement and parse.
• CSV is processed by almost all existing applications.
Format of a CSV file :
BEFORE
EXCUTION
OUTPU AFTER
T EXECUTION
Example : Writing data to CSV
file
myfile.cs
v
Example : Writing data to CSV
file
myfile.csv
BEFORE
EXECUTION
myfile.csv
OUTPU AFTER
T EXECUTION
EXAMPLE PROGRAM TO WRITE ON TO THE CSV FILE
import csv
fields=['Rno','Name','Avg']
rows=[ ['1', 'Abhishek' , '98'],
Content of Student.csv
['2', 'Anand', '99'], Rno,Name,Avg
['3', 'Ravi', '88']]
1,Abhishek,98
with open("Student.csv","w") as fout:
csvwriter=csv.writer(fout) 2,Anand,99
csvwriter.writerow(fields)
3,Ravi,88
print("Column headings written")
csvwriter.writerows(rows)
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE
myfile.cs
v
Example : Counting number of
records
myfile.csv
Example : Sum of Salary and counting
employee getting more than 70000
Example : Sum of Salary and counting
employee getting more than 70000
myfile.csv
DIALECT PARAMETER IN
READER() AND WRITER()
• It is a construct that allows to create, store and
re-use various formatting parameter for the data.
• Python offers 2 ways to specify formatting
parameters.
(i) By declaring a sub class of this class which
contains the specific attributes.
(ii) By directly specifying the formatting
parameters, using the same names as defined in
the Dialect class.
DIALECT PARAMETER IN READER() AND WRITER()
Dialects support the following attributes:
Dialect.strict When True, raise exception Error on bad CSV input. The default is
False.
EXAMPLE PROGRAM TO READ FROM
THE CSV FILE
def readstudent():
with open("Student.csv") as fin:
reader=csv.reader(fin)
for row in reader:
print(row)
readstudent()
OUTPUT:
['Rno', 'Name', 'Avg']
['1', 'Abhishek', '98']
['2', 'Anand', '99']
['3', 'Ravi', '88']
CHANGING THE DELIMITER
def readstudent():
with open("Student.csv") as fin:
reader=csv.reader(fin, delimiter='/',
quoting=csv.QUOTE_NONE)
for row in reader:
print(row)
readstudent()
OUTPUT:
with open("Student1.csv") as fin: reader=csv.reader
["'Rno','Name','Avg'"]
(fin, dialect="myDialect") for row in reader: ['1', 'Abhishek', '98']
print(row) ['2', 'Anand', '98']
['3', 'AAAA', '88']
readstudent()
OUTPUT:
France
Italy Spain
Russia
India
EXAMPLE PROGRAM USING DICTREADER
• Write a python program to read from the file
emp.csv and display the name of the employees
whose salary >50000