A few words of thanks would be greatly appreciated.

PYTHON - Read and Write CSV files

It’s possible to read and write CSV (Comma Separated Values) files using Python 2.4 Distribution. Like most languages, file operations can be done with Python. This can be done with Python by importing the CSV module and creating a write object that will be used with the WriteRow Method. Reading a CSV file can be done in a similar way by creating a reader object and by using the print method to read the file. As file operations require advanced concepts, some knowledge of programming with Python is required to read and write the CSV.

Python 2.4

Python 2.4 supports CSV format.

The Reference Library is very helpful when learning to use Python. Here are some additional tips to get started.


  • Knowledge of Python
  • Python 2.4 Distribution or recent.

Writing a CSV file

Start by importing the CSV module:

import csv

We will define an object "writer" (called c) which can later be used to write the CSV file:

c = csv.writer(open("MYFILE.csv", "wb"))

Now we will apply the method to write a row. Writerow (method) takes one argument - this argument must be a list and each list item is equivalent to a column. Here, we try to make an address book:


Then we will save all entries in this way.

Reading a CSV file

First just create an object reader (we will call it: cr).

cr = csv.reader(open("MYFILE.csv","rb"))

And here we get each row (in the form of a list of columns) as follows:

for row in cr:    
    print row

We can extract a precise entry of a row with the index:

for row in reader:    
    print row[2], row[-2]

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « PYTHON - Read and Write CSV files », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).