Python Read Write CSV File

In this post we’ll look at how to read and write CSV files in Python. The examples use the CSV module and Pandas.

Python Read CSV File Using CSV Module

This code example reads a file named orders.csv and loops through the data:

import csv

f = open("orders.csv", "rt")
orders = csv.reader(f)
for order in orders:
    print(order)
f.close()

Output of orders.csv:

['OrderID', 'CustomerID', 'OrderDate']
['10248', '4', '10/02/2020']
['10249', '2', '10/02/2020']
['10250', '7', '10/02/2020']

Read CSV File as Dictionary

Using the DictReader method each row in the CSV file is represented as a row in the dictionary, with the column header being the key.

import csv

orders = csv.DictReader(open("orders.csv"))
for order in orders:
    print(order)

Output of orders.csv:

{'OrderID': '10248', 'CustomerID': '4', 'OrderDate': '10/02/2020'}
{'OrderID': '10249', 'CustomerID': '2', 'OrderDate': '10/02/2020'}
{'OrderID': '10250', 'CustomerID': '7', 'OrderDate': '10/02/2020'}

Read CSV File Using Pandas

To use Pandas, we first need to install the Pandas library.

To install, run this command: pip3 install pandas.

import pandas

orders = pandas.read_csv('orders.csv')
print(orders)

Output of orders.csv:

OrderID  CustomerID   OrderDate
10248           4     10/02/2020
10249           2     10/02/2020
10250           7     10/02/2020

Python Write CSV Using Pandas

from pandas import DataFrame
import pandas as pd

order = pd.DataFrame({'OrderID': ['10251', '10252', '10253'],
                   'CustomerID': ['5', '1', '8'],
                   'OrderDate': ['11/02/2020', '11/02/2020', '11/02/2020']})
order.to_csv('newOrders.csv', index=False)

Output of newOrders.csv:

OrderID,CustomerID,OrderDate
10251,5,11/02/2020
10252,1,11/02/2020
10253,8,11/02/2020

Append Data To Existing CSV File

The default mode when writing to csv file is 'w'. If we want to append data to an existing CSV file we have to use the append mode, e.g. mode='a'

from pandas import DataFrame
import pandas as pd

order = pd.DataFrame({'OrderID': ['10254'],
                   'CustomerID': ['3'],
                   'OrderDate': ['11/02/2020']})
order.to_csv('newOrders.csv', mode='a', index=False, header=False)

Output of newOrders.csv:

OrderID,CustomerID,OrderDate
10251,5,11/02/2020
10252,1,11/02/2020
10253,8,11/02/2020
10254,3,11/02/2020

Python Write CSV Using CSV Module

N.B. The first row is used as the column header

import csv
with open('orders.csv', 'w', newline='') as file:
    order = csv.writer(file)
    order.writerow(['OrderID', 'CustomerID', 'OrderDate'])
    order.writerow(['10251', '6', '11/02/2020'])
    order.writerow(['10252', '9', '11/02/2020'])
    order.writerow(['10253', '5', '11/02/2020'])

Output of orders.csv:

OrderID,CustomerID,OrderDate
10251,6,11/02/2020
10252,9,11/02/2020
10253,5,11/02/2020

If you want to append to an existing csv file using the csv module, you need to pass in 'a' parameter to the open() method. You also need to skip the “headings”.

For example:

with open('orders.csv', 'a', newline='') as file:
    order = csv.writer(file)
    order.writerow(['10251', '6', '11/02/2020'])

Write CSV File with DictWriter

We can also use the DictWriter method of the csv class to create and write a CSV file.

import csv
with open('orders.csv', 'w', newline='') as file:
    fieldnames = ['OrderID', 'CustomerID', 'OrderDate']
    order = csv.DictWriter(file, fieldnames=fieldnames)
    order.writeheader()
    order.writerow({'OrderID': '10251', 'CustomerID': 7, 'OrderDate': '11/02/2020'})
    order.writerow({'OrderID': '10252', 'CustomerID': 3, 'OrderDate': '11/02/2020'})
    order.writerow({'OrderID': '10253', 'CustomerID': 1, 'OrderDate': '11/02/2020'})

Output of orders.csv:

OrderID,CustomerID,OrderDate
10251,7,11/02/2020
10252,3,11/02/2020
10253,1,11/02/2020