Reading and Writing CSV files in Python

Source:www.opentechguides.com | Date Published: 2019-04-08 11:39:51

CSV (Comma Separated Values) format is one of the most popular file format to store tabular data (data in rows and columns) in a text file. The values in a CSV file are separated by a delimiter which is most commonly a comma (,). The csv module in python implements functions and classes to read and write data in CSV files. Here are some examples of using csv module in your Python program to read and write CSV files.

Reading data from CSV file

Consider we have a CSV file named fruits.csv with the following contents.

Apple,30
Orange,20
Grapes,25
Mango,40

Here is a Python program to read fruits.csv file and print its contents.

import csv

with open('fruits.csv' , newline='') as csv_file:
    csvreader = csv.reader(csv_file, delimiter=',')
    for row in csvreader:
        print(row)

First of all, import the CSV module using the import statement. Next is to open the file for reading using the built-in open() function.

The with statement encapsulates the commonly used try...catch...finally pattern and ensures that if an exception is raised in the with block of code then the file is closed automatically and you don't have to call the close() function.

The reader function of the csv module returns a reader object which contains a list of string elements for each row in the CSV file.

Finally, each row of data is printed on a for loop. The resulting output will be as below:

['Apple', '30']
['Orange', '20']
['Grapes', '25']
['Mango', '40']

Reading CSV file into a data dictionary

Instead of reading the CSV data as a list of string elements you may read the data into an ordered dictionary (collection of key-value pairs) using the DictReader() function. The keys for the dictionary items can be specified using the fieldnames parameter to DictReader() function. If the fieldnames parameter is omitted, the values in the first row of the CSV file are used as the key names.

Below Python code reads the contents of fruits.csv file to a dictionary.

import csv
with open('fruits.csv', 'r', newline='') as csv_file:
    dict_reader=csv.DictReader(csv_file, fieldnames=['Name','Quantity'])
    for row in dict_reader:
        print(row)

The output of the above script will be:

OrderedDict([('Name', 'Apple'), ('Quantity', '30')])
OrderedDict([('Name', 'Orange'), ('Quantity', '20')])
OrderedDict([('Name', 'Grapes'), ('Quantity', '25')])
OrderedDict([('Name', 'Mango'), ('Quantity', '40')])

Writing data to a CSV file

Similar to the reader function, there is a writer function in the csv module that returns a writer object which converts data to CSV format and writes to a file.

import csv

with open('fruit_stock.csv', 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file, delimiter=',')
    data=[['Apple','30'], ['Orange', '20'],['Grapes', '25'], ['Mango', '40']]
    csv_writer.writerows(data)

You pass a set of row elements to writerrows method and it will write those rows to a file according to the current dialect (Learn more about Dialects). The above Python program creates a CSV file named fruit_stock.csv and contains the following data.

Apple,30
Orange,20
Grapes,25
Mango,40

You may also use the writerow() method instead of writerows() to write a single row at a time. For example, like

csv_writer.writerow(['Apple','30'])
csv_writer.writerow([Orange,'20'])

Writing a dictionary to CSV file

You can write an ordered dictionary of key-value pairs to the csv file using the DictWriter function. Here is an example.

import csv

with open('fruits_stock.csv', 'w', newline='') as csv_file:
    dict_writer = csv.DictWriter(csv_file, fieldnames = ['Name','Quantity'])
    dict_writer.writeheader()
    dict_writer.writerow({'Name': 'Apple', 'Quantity': '30'})
    dict_writer.writerow({'Name': 'Orange', 'Quantity': '20'}])
    dict_writer.writerow({'Name': 'Grapes', 'Quantity': '25'}])
    dict_writer.writerow({'Name': 'Mango', 'Quantity': '40'}])									

When the above Python program is run, the file fruits_stock.csv will have the following data:

Name,Quantity
Apple,30
Orange,20
Grapes,25
Mango,40

To write multiple rows, call the writerows() method instead or writerow() as below:

dict_writer.writerows([{'Name': 'Apple', 'Quantity': '30'}, {'Name': 'Orange', 'Quantity': '20'}])

The csv module in Python offers a simple mechanism to read and write data in CSV format. If you need to perform complex analysis or manipulations of the csv data then Pandas library will be a better choice.


Open Tech Guides | www.opentechguides.com