1
CSV FILES
Monday , 11.09.2023
• import csv module
• Open / Close csv file,
• Write into a csv file using csv.writer()
• Read from a csv file using csv.reader( )
Learning Objective : To interface Python code to CSV Files
2
CSV files
• CSV (Comma Separated Values) is a simple file format used to store tabular data ( such as a
spreadsheet or database ) in plain text.
• 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.
• Comma-separated file is a delimited text file that uses a comma to separate values.
Delimited Text File means
• each line represents a record &
• the fields of each record are
separated by a delimiter
• Eg:- 12345, Alin Raj, 95.4
• 2145 , Betty Sam, 92.5
Learning Objective : To interface Python code to CSV Files
Features 3
ADVANTAGE OF CSV FILE
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.
Learning Objective : To interface Python code to CSV Files
Features 4
CSV FILE HANDLING IN PYTHON
import csv
• To perform read and write operation with CSV file, we must import csv module.
• CSV module can handle CSV files correctly regardless of the operating system on which the files were
created.
Learning Objective : To interface Python code to CSV Files
5
FUNCTIONS IN CSV MODULE
• csv.reader( ) - Return a reader object which will iterate over lines in the given csvfile.
• csv.writer( ) - Return a writer object responsible for converting the user’s data into
delimited strings on the given file object.
• next( ) - Return the next row of the reader’s iterable object as a list (if the object was
returned from reader())
• csvW.writerow(row) - Write the row parameter to the writer’s file object.
• csvW.writerows(rows) - Write all elements in rows (to the writer’s file object.
Learning Objective : To interface Python code to CSV Files
WRITE TO CSV FILE - writerow( ) and writerows( )
Using writerow( ) Using writerows( )
Learning Objective : To interface Python code to CSV Files 6
7
READ THE CONTENTS OF CSV FILE
Learning Objective : To interface Python code to CSV Files
8
ACTIVITY D2
1. WAP to append data to a CSV file ‘Payroll.csv’ that must contain the employee details – EmpNo, Name and
Salary. The column heading must be given at the start.
Learning Objective : To interface Python code to CSV Files
ACTIVITY D2
2. Write a function to read and display data from a CSV File ‘Payroll’. (Display as list as well as in tabular
format)
Learning Objective : To interface Python code to CSV Files
ACTIVITY D2
3. WAF to search and display details of those employees with Salary >=10000.
Learning Objective : To interface Python code to CSV Files
Objective : The CSV file ‘payroll’ contains the following
RECORD FILE – information :
D.4 EMPLOYEE NO, NAME, SALARY
PAYROLL.CSV Write a menu-driven program in Python to perform the following
tasks:
• Enter details
• Display details
• Search and display Employee details with Salary >=10000
• Concept Used : CSV Files
Learning Objective : To interface Python code to CSV Files
RECORD FILE – Objective : The CSV file ‘login’ contains the User name and
Password.
D.5
Write a menu-driven program in Python to perform the following
login.csv tasks:
• Enter details
• Display details
• Search and display the password of a username.
(NB : Ensure that a user name is unique)
• Concept Used : CSV Files
Learning Objective : To interface Python code to CSV Files