Working with CSV files in Python

A common file format used to store data is the .csv file. The ‘csv’ in the name stands for “Comma Separated Values”. CSV files are very similar to Excel spreadsheets in that the data theiris arranged in rows and columns. Excel files are written in binary format whereas csv files are simple text files that can be read using a text editor. It’s easier for programs to parse text files compared to binary file types like excel docs and PDFs.

In this article, I will introduce the Python csv module and show you how to use it to read and write data to csv files. Support for csv is built-in to Python so there is no need to download any extra packages.

Reading data from a CSV file

The first thing to do is to import the csv module and create a csv reader object from a csv file:

import csv

>>> with open('SalesJan2009.csv', 'r') as csv_file:
        reader = csv.reader(csv_file, delimiter=',')
	for row in reader:
	    print ', '.join(row)

Transaction_date, Product, Price, Payment_Type, Name. City
1/2/09 6:17, Product1, 1200, Mastercard, Stacy, New York 
1/2/09 4:53, Product7, 200, Visa, Janet, Ottawa
1/2/09 13:08, Product3, 1200, Mastercard, Barbara, Hyderabad
1/3/09 14:44, Product111, 1500, Visa, Sabine, London 
>>> 

In the code sample above, we imported the csv module, then created a file object that represents our CSV file. We then passed the file object to the csv.reader() function which returns a reader object. The reader object allows us to manipulate the csv file in the same way we can manipulate normal file objects. Because of this, it is possible to iterate over rows in the CSV document.

Reading Specific Columns

You can also retrieve data from named columns in csv files using the DictReader class. The data in the csv file I’m usng here are customer details for an Online shop. Suppose we were only interested the names of the customers and the cities they are from:

import csv

with open('SalesJan2009.csv', 'r') as the_file:
	reader = csv.DictReader(the_file, delimiter=',')
	for line in reader:
		print line["Name"],line["City"]

Stacy New York                        
Janet Ottawa
Barbara Hyderabad
Sabine London

Writing data to a CSV file.

To write data to a csv file you can either use the Writer() function or the DictWriter() function. The example below illustrates how to use the writer() function.

import csv

names_file = open('names.csv', 'w')
writer = csv.writer(names_file)
writer.writerow(['Stacy', 'Janet', 'Sabine', 'Joshua'])
writer.writerow(['New York', 'Ottawa', 'London', 'Harare'])

names_file.close()

The writer() function takes a file object as an argument and creates a writer object. We use Its writerow() method to write each row of the csv file. Lastly we save our changes to disk.

I did not cover how to write to csv files using DictWriter() function, maybe I will in a future article. Thanks for reading.

1 thought on “Working with CSV files in Python”

Comments are closed.