Python: CSV Read and Write

CSV (Comma Separated Values)?

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. 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.

How to handle CSV file records in Python?

In Python, there is a library/module named csv which is used to handle CSV files. It consists of methods for read and write operations.

In order to open a CSV file we use open() method. This method accepts two mandatory arguments which are the file name and the mode.

They are three different modes:

  1. Read mode – ‘r‘ is used to open a file in read mode.
  2. Write mode – ‘w‘ is used to open a file in write mode.
  3. Append mode – ‘a‘ is used to open a file in append mode.

Note: Whenever you try to open a file which is not existing on the file system, a new file will be created by the open method. The difference between write mode and append mode is ‘In write mode, the existing contents of the files will be deleted whereas in append mode the existing contents will be retained‘.

The csv module consists of methods that are useful to perform read and write operations.

Read Methods

  • reader() – reader  method accepts file object as an argument and returns a reader object. We can read each record as a list where each element in the list represents a column value.
  • dictReader() – DictReader() accepts file object as an argument and returns a DictReader object. Each record of csv file will be fetched as dictionary where keys represent the column names and values are their corresponding values.

Write Methods

  • writer() – writer method accepts file object as an argument and returns a csv writer object. We need to use writerow method in order to insert records. Lists are used to insert the data into file.
  • dictWriter() – DictWriter accepts filename and field names as arguments. Data which needs to be inserted should be in dictionary format and the keys of dictionary should match with the field names.

Sample CSV File Data

In further example of csv, we are going to use the following data. You can use same by place in text file and change extension of file with .csv

id,name,salary,start_date,dept
1,Saurabh,563.3,2018-01-15,IT
2,Gaurav,326.2,2016-09-23,Operations
3,Ramesh,360,2018-11-15,IT
4,Rajesh,729,2019-05-11,HR
5,Ranjith,986.25,2021-03-27,Finance
6,Rajendra,796,2018-05-21,IT
7,Ajay,423.8,2016-07-30,Operations
8,Bharti,934.5,2015-06-17,Finance

Sample CSV File

Here in the above data first-row, comma-separated values represent column name while the rest of the rows are data for each column.

Reading a CSV File

As explained in the above method definition. You can use these lines of statement to read CSV File.

import csv
   with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.reader(csvfile)
    for record in reader:
        print(record)

Output

[‘id’, ‘name’, ‘salary’, ‘start_date’, ‘dept’]
[‘1’, ‘Saurabh’, ‘563.3’, ‘2018-01-15’, ‘IT’]
[‘2’, ‘Gaurav’, ‘326.2’, ‘2016-09-23’, ‘Operations’]
[‘3’, ‘Ramesh’, ‘360’, ‘2018-11-15’, ‘IT’]
[‘4’, ‘Rajesh’, ‘729’, ‘2019-05-11’, ‘HR’]
[‘5’, ‘Ranjith’, ‘986.25’, ‘2021-03-27’, ‘Finance’]
[‘6’, ‘Rajendra’, ‘796’, ‘2018-05-21’, ‘IT’]
[‘7’, ‘Ajay’, ‘423.8’, ‘2016-07-30’, ‘Operations’]
[‘8’, ‘Bharti’, ‘934.5’, ‘2015-06-17’, ‘Finance’]

If you noticed in the above example all the rows of CSV file read including column header and got printed.

Read CSV File by using DictReader

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input.csv','r') as csvfile:
    reader=csv.DictReader(csvfile)
    for record in reader:
        print(record) 

Output

OrderedDict([(‘id’, ‘1’), (‘name’, ‘Saurabh’), (‘salary’, ‘563.3’), (‘start_date’, ‘2018-01-15’), (‘dept’, ‘IT’)])
OrderedDict([(‘id’, ‘2’), (‘name’, ‘Gaurav’), (‘salary’, ‘326.2’), (‘start_date’, ‘2016-09-23’), (‘dept’, ‘Operations’)])
OrderedDict([(‘id’, ‘3’), (‘name’, ‘Ramesh’), (‘salary’, ‘360’), (‘start_date’, ‘2018-11-15’), (‘dept’, ‘IT’)])
OrderedDict([(‘id’, ‘4’), (‘name’, ‘Rajesh’), (‘salary’, ‘729’), (‘start_date’, ‘2019-05-11’), (‘dept’, ‘HR’)])
OrderedDict([(‘id’, ‘5’), (‘name’, ‘Ranjith’), (‘salary’, ‘986.25’), (‘start_date’, ‘2021-03-27’), (‘dept’, ‘Finance’)])
OrderedDict([(‘id’, ‘6’), (‘name’, ‘Rajendra’), (‘salary’, ‘796’), (‘start_date’, ‘2018-05-21’), (‘dept’, ‘IT’)])
OrderedDict([(‘id’, ‘7’), (‘name’, ‘Ajay’), (‘salary’, ‘423.8’), (‘start_date’, ‘2016-07-30’), (‘dept’, ‘Operations’)])
OrderedDict([(‘id’, ‘8’), (‘name’, ‘Bharti’), (‘salary’, ‘934.5’), (‘start_date’, ‘2015-06-17’), (‘dept’, ‘Finance’)])

Writing to CSV File

Here is the line of code to write to a CSV File. To write in CSV file use mode as ‘a’ to append to an existing file if you want to rewrite the complete new CSV file use mode as ‘w’.

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input1.csv','w') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(['id','name','salary','dept'])
	    writer.writerow([1,'Sunny',22001.00,'Operation'])
	    writer.writerow([2,'Jony',26501.00,'HR'])


Output

Write to csv with new line

If you have observed the above output, we get a blank line between every record. This is because writerow method inserts a newline after every insertion and also open method also inserts a newline character. It can be removed as follows by adding newline=” in open method:

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input1.csv','w',newline='') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(['id','name','salary','dept'])
	    writer.writerow([1,'Sunny',22001.00,'Operation'])
	    writer.writerow([2,'Jony',26501.00,'HR'])


Output

CSV file without new line

Write to CSV file using DictWriter

import csv
with open(r'C:\Users\saurabh.gupta14\Desktop\Python Example\input2.csv','w',newline='') as csvfile:
	    fields=['id','name','salary','dept']
	    writer=csv.DictWriter(csvfile,fields)
	    writer.writeheader()
	    writer.writerow({'id':1,'name':'Raghav','salary':22001.00,'dept':'HR'})
	    writer.writerow({'id':2,'name':'Rajendra','salary':26501.00, 'dept':'Opearation'})

Note: The key of dictionary should exactly map with the name in the fields name.

Output

Summary

In this blog you have learn about the different cases of CSV handling through Python csv module.

  • Read operation through csv
  • Write operation through csv