CSV module for Python

Documentation

The parser supports linebreaks inside quoted fields. To use the module, you create a parser object then pass lines of CSV text to the parser. When a record has been parsed, the parser will return a list of fields.

Typical use of the module will look like:

import csv
p = csv.parser()
file = open("afile.csv")
while 1:
    line = file.readline()
    if not line:
        break
    fields = p.parse(line)
    if not fields:
        # multi-line record
        continue
    # process the fields

The parser constructor has the following interface:

parser(ms_double_quote = 1, field_sep = ',', auto_clear = 1)
The parser object contains the following attributes:
fields The fields recognised from the current record. If an exception is raised during record parsing, this attribute can be inspected to retrieve the fields which were successfully parsed.
ms_double_quote Controls MS-style double quote handling. When this is non-zero, two consecutive " characters will be translated into a single " character. By default, this is set to 1.
field_sep The field separator. Defaults to ',' but can either be supplied in the constructor as a keyword argument, or can be set by assigning to this attribute.
auto_clear When auto_clear is TRUE calling parse() will automatically call the clear() method if the previous call to parse() raised an exception during parsing. Defaults to TRUE but can either be supplied in the constructor as a keyword argument, or can be set by assigning to this attribute.
had_parse_error A read only flag which is TRUE if the last call to parse() raised an exception during parsing.

The parser object contains the following methods:
clear() Discards all fields parsed so far. You should call this after a parser exception.
parse(string) -> list of strings Extracts fields from the (partial) CSV record in str. Trailing end of line characters are ignored, so you do not need to strip the string before passing it to the parser. If you pass more than a single line of text, a csv.Error exception will be raised.
join(sequence) -> string Construct a CSV record from a sequence of fields. Non-string elements will be converted to string.

The parser will raise a csv.Error exception under any of the following circumstances: