CSV files
CSV files
CSV stands for Comma Separated Value, and CSV files are delimited text files that stores data
values separated with a delimiter. The default delimiter is comma (,) however other delimiters
like pipe(|), colon(:) , tab (\t) etc may also be used.
As CSV files are text files, they are human readable.
Data is stored in tabular format (as in a spreadsheet). Each comma separated value represents
a field and each line represents a record.
CSV files are popular formats used for import and export of data between different
applica ons.
Important Note:
1. To indicate a file as CSV file it should be stored with .csv extension (eg. Student.csv)
2. CSV files should be opened with newline argument set to null / empty string to suppress
transla on of the EOL character (newline=‘ ’)
3. File opening modes are same as that of normal text files.
What are the benefits of using a CSV file? OR Why are CSV popular for data storage?
1. CSV files are the preferred file format for import and export of data between different
applica ons. That means CSV file can be opened in text editors as well as spreadsheet or any
database applica on too (as each comma separated value in a CSV file represents a field and
each line of text represents a record).
2. CSV files are type of text file, easier to create and are human readable.
3. CSV files are capable of storing large amount of data.
Page 1 K. Hazra
Which module needs to be imported to work with CSV files?
How data is wri en / read in CSV files? OR What is the need of / func on of writer and reader
objects in a CSV file?
1. For wri ng data to a CSV file, writer( ) method of the csv module is used to create a writer
object. Writer object is responsible to convert user data into delimited text format to be
wri en into the CSV file. Data (formed as a Python sequence in the form of tuple or list) is
wri en to the CSV file using the writerow( ) or writerows( ) method.
writerow( ) method of the writer object takes a data records (in the form of list / tuple) as
parameter to write a single record at a me to the CSV file.
writerows( ) method takes mul ple data records (in the form of nested list or tuple) and write
them at once.
2. For reading data from a CSV file, a reader object is created using the reader( ) method. The
reader object reads data from a CSV file, parses it (converts) and makes the data available
as an iterable object from where data can be read record/line/row by record/line/row.
(each line represents a record of comma separated values as it was wri en)
Programming Examples
1. Write a program to store Student data (Roll, Name & Mark) of five students to a CSV file
StudData.csv using writerow()
Output
Page 2 K. Hazra
Content of StudData.csv (in Notepad and Ms-Excel)
Note line gap in Excel between records, as the EOL gets translated to newline.
1. Add two more records to StudData.csv, (se ng newline= ‘ ’ to empty string to suppress
transla on of EOL)
Content of StudData.csv (in Notepad and Ms-Excel, there no extra lines between records)
Page 3 K. Hazra
3. Write a program to store records using writerows() to StudData.csv file
4. Write a program to read all students data from a CSV file StudData.csv
4. Write a program to read students data from a CSV file StudData.csv and display Names of those
students who have scored more than 95
Page 4 K. Hazra
5. Write a program to read students data from a CSV file StudData.csv and display Names and
Marks of those students star ng with the le er ‘S’.
Page 5 K. Hazra