The csv Module

(New in 2.3) The csv module is used to read data files in the CSV (comma-separated values) format, as used by Microsoft Excel and many other applications. A CSV file contains a number of rows, each containing a number of columns, usually separated by commas. An example:

 
Title,Release Date,Director
And Now For Something Completely Different,1971,Ian MacNaughton
Monty Python And The Holy Grail,1975,Terry Gilliam and Terry Jones
Monty Python's Life Of Brian,1979,Terry Jones
Monty Python Live At The Hollywood Bowl,1982,Terry Hughes
Monty Python's The Meaning Of Life,1983,Terry Jones

To read this file, you could simply loop over the lines, and use the split method to get the individual columns:

for line in open("samples/sample.csv"):
    title, year, director = line.split(",")
    print year, title

Using the csv module instead, you can write:

# File: csv-example-1.py

import csv

reader = csv.reader(open("samples/sample.csv"))

for title, year, director in reader:
    print year, title
$ python csv-example-1.py
Release Date Title
1971 And Now For Something Completely Different
1975 Monty Python And The Holy Grail
1979 Monty Python's Life Of Brian
1982 Monty Python Live At The Hollywood Bowl
1983 Monty Python's The Meaning Of Life

The reader function takes sequence or an iterable file object, and returns an iterator.

The parser used by the csv module does a lot more than just splitting strings on commas. It handles quoted values and line endings properly, so you can read files that contain arbitrary strings (including strings that contain commas). It’s also configurable; you can create and register “dialect” descriptors, and use them to read different CSV flavours.

For example, here’s how to read the Swedish variant of CSV, which uses semicolons instead of commas:

# File: csv-example-2.py

import csv

class SKV(csv.excel):
    # like excel, but uses semicolons
    delimiter = ";"

csv.register_dialect("SKV", SKV)

reader = csv.reader(open("samples/sample.skv"), "SKV")

for title, year, director in reader:
    print year, title

Note that if you only need to modify a few parameters, you can pass them as keyword arguments to the reader function:

 
# File: csv-example-3.py

import csv

reader = csv.reader(open("samples/sample.skv"), delimiter=";")

for title, year, director in reader:
    print year, title

The module can also be used to create CSV files. The writer function creates a CSV writer object, which converts values to strings and escapes them properly.

 
# File: csv-example-4.py

import csv
import sys

data = [
    ("And Now For Something Completely Different", 1971, "Ian MacNaughton"),
    ("Monty Python And The Holy Grail", 1975, "Terry Gilliam, Terry Jones"),
    ("Monty Python's Life Of Brian", 1979, "Terry Jones"),
    ("Monty Python Live At The Hollywood Bowl", 1982, "Terry Hughes"),
    ("Monty Python's The Meaning Of Life", 1983, "Terry Jones")
]

writer = csv.writer(sys.stdout)

for item in data:
    writer.writerow(item)
$ python csv-example-4.py
And Now For Something Completely Different,1971,Ian MacNaughton
Monty Python And The Holy Grail,1975,"Terry Gilliam, Terry Jones"
Monty Python's Life Of Brian,1979,Terry Jones
Monty Python Live At The Hollywood Bowl,1982,Terry Hughes
Monty Python's The Meaning Of Life,1983,Terry Jones

If you have data in a sequence (or any other iterable object), you can use the writerows method instead:

writer = csv.writer(sys.stdout)
writer.writerows(data)

The writer function also supports different dialects.

 

A Django site. rendered by a django application. hosted by webfaction.