VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
NO.1 TEZPUR
COMMA SEPARATED
VALUE
CSV FILE
CSV is a simple file format used to store tabular data,
such as a spreadsheet or database.
Files in the CSV format can be imported to and
exported from programs that store data in tables,
such as Microsoft Excel or OpenOffice Calc.
CSV stands for "comma-separated values“.
A comma-separated values file is a delimited 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
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
CSV file handling in Python
To perform read and write operation with
CSV file, we must import csv module.
open() function is used to open
file, and return file object.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Reading from CSV file
import csv module
Use open() to open csv file, it will return
file object.
Pass this file object to reader object.
Perform operation you want
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Reading from CSV
File
myfile.cs
v
OUTPU
T
GO TO NEXT SLIDE TO UNDERSTAND THE CODE LINE BY
LINE
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Reading from CSV
File
myfile.cs
v
First line :- is to import csv module, this is necessary to perform
operations on csv file Second line :- is to open file to read using “with”
block, this block will ensure opened file will be closed automatically
after this block even in case of runtime error also.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Reading from CSV
File
myfile.cs
v
Third line: to create reader object, to perform read operation on csv
file, the first parameter is the name of csv file, and second
parameter is delimiter i.e. on what basis value of different field to
read, by default it is comma(,) so this is optional if not given it will be
by default comma, however if any other character is used in file then
we must
VINOD KUMARspecify this. myreader
VERMA, PGT(CS), will
KV OEF KANPUR become
& SACHIN csv reader
BHARDWAJ, PGT(CS),object
KV NO.1to read
TEZPUR
Example : Reading from CSV
File
myfile.cs
v
Fourth line : this is formatted string to specify width of each column, so
that output appears properly aligned in their width, here %10s
means 10 space for EMPNO, and so on. This is helpful in printing output
in formatted way.
Sixth line :- this line is used to read each row from file one by one and
storeKUMAR
VINOD in row
VERMA,variable.
PGT(CS), KV i.e. all the
OEF KANPUR comma
& SACHIN separated
BHARDWAJ, PGT(CS),values
KV NO.1will be
TEZPUR
Example : Reading from CSV
File
myfile.cs
v
Seventh Line :- this line is simply printing all the values in row
variable by specifying index i.e. index 0 will be for (according to first
row in file) 1, 1 for „Amit‟, 2 for 6000. here also we used same width
as for heading so that both heading and data are aligned properly
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
Example : Reading from CSV
File
OUTPU
T
Here you can see the
output is properly
aligned in their
GO TO NEXT SLIDE TO KNOW HOW TO CREATE CSV
assigned width FILE
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
How to create CSV
file
Method 1 (From MS-Excel):
🞑 Open Excel, delete all the sheet except sheet 1
🞑 Type all the data, in separate cells
🞑 Save it as csv file in your desired location.
🞑 If any warning comes, click on „YES‟
🞑 When you close the excel, choose „NO‟
🞑 Now file is created at your desired location, go
and double click or open with notepad to check
the content
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
How to create CSV
file
Method 2 (From Notepad):
🞑 Open Notepad
🞑 Type record by separating each column value by
comma(,)
🞑 Every record in separate line
🞑 Save it by giving extension .csv (PUT THE NAME IN
DOUBLE
QUOTES TO ENSURE .TXT WILL NOT BE APPENDED
WITH
FILE NAME FOR E.G. if you want it to save with
name emp then give name as “emp.csv” in
double quotes
FileKUMAR
🞑VINOD is created close
VERMA, PGT(CS), itKANPUR
KV OEF and &double click PGT(CS),
SACHIN BHARDWAJ, to open KV NO.1
Example : Counting number of
records
myfile.cs
v
OUTPU
T
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Sum of Salary and counting
employee getting more than
7000
myfile.c
sv
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Sum of Salary and counting
employee getting more than
7000
myfile.cs
v
OUTPU
T
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Writing date in CSV file
import csv module
Use open() to open CSV file by
specifying mode “w” or “a”, it
will return file object.
“w” will overwrite previous content
“a” will add content to the end of
previous content.
Pass the file object to writer object with
delimiter.
Then use writerow() to send data in CSV
file
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Writing data to
CSV file
myfile.cs
v
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Writing data to
CSV file
First line : importing csv module to perform operation on csv file.
Second line: to open file for writing in append mode using
“with” block,
already discussed with read program.
Third line: to create csv writer object to write in csv file, first
parameter is the name of file object, second one is optional
parameter i.e. delimiter. Here mywriter will be the writer object
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Writing data to
CSV file
Fourth line: we have take a variable ans=„y‟ so that loop will
iterate as long as ans value is „y‟
Fifth line: we have create a while loop using ans i.e. as long as
ans is „y‟ loop will iterate.
Next 3 lines : we have taken input for eno, name and salary
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Example : Writing data to
CSV file
Ninth line : in this line we are writing data to csv file using
writerow() of mywriter in the form of list. This function takes
any iterable object to write like list, tuples, etc. So we have
passed all the input in the form of list.
Another function is writerows() which is used when we want to
write multiple
rows.
Next two
VINODlines are
KUMAR simple
VERMA, printing
PGT(CS), KV OEFmessage
KANPUR & and taking
SACHIN input
BHARDWAJ, in KV NO.1
PGT(CS),
Example : Writing data to
CSV file
myfile.cs
BEFORE v
EXECUTION
myfile.cs
OUTPU AFTER v
T EXECUTION
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
Program to create CSV file and store empno,name,salary
and search any empno and display name, salary and if not
found appropriate message.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1
OUTPUT
Enter Employee Number 1 Enter Employee Number to search :2
Enter Employee Name ====================
Amit Enter Employee ========
NAME :
Salary :90000 # # Data Sunil
Saved... ## SALARY : 80000
Add More ?y Search More ? (Y)y
Enter Employee Number 2 Enter Employee Number to search :3
Enter Employee Name ====================
Sunil Enter Employee ========
Salary :80000 # # Data NAME :
Saved... ## Satya
Add More ?y SALARY :
Enter Employee Number 3 75000
Enter Employee Name Search More ?
Satya Enter Employee (Y)y
Salary :75000 # # Data Enter
Saved... ## Employee
Add More ?n Number to
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1