0% found this document useful (0 votes)
34 views11 pages

005.2 CSV

Hi

Uploaded by

shivensinha816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views11 pages

005.2 CSV

Hi

Uploaded by

shivensinha816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

for more updates visit: www.python4csip.

com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

COMMA SEPARATED VALUE


for more updates visit: www.python4csip.com

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
for more updates visit: www.python4csip.com

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 TEZPUR
for more updates visit: www.python4csip.com

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 TEZPUR
for more updates visit: www.python4csip.com

Example : Reading from CSV File

myfile.csv

OUTPUT

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

Example : Counting number of records

myfile.csv

OUTPUT

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

Example : Sum of Salary and counting employee


getting more than 7000

myfile.csv

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

Example : Sum of Salary and counting employee


getting more than 7000

myfile.csv

OUTPUT

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

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 TEZPUR
for more updates visit: www.python4csip.com

Example : Writing data to CSV file

myfile.csv

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

Example : Writing data to CSV file

myfile.csv
BEFORE EXECUTION

myfile.csv
OUTPUT AFTER EXECUTION

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

You might also like