CSVParser

CSVParser.py

A parser for CSV files.

class MiscUtils.CSVParser.CSVParser(allowComments=True, stripWhitespace=True, fieldSep=',', autoReset=True, doubleQuote=True)

Bases: object

Parser for CSV files.

Parses CSV files including all subtleties such as:

  • commas in fields

  • double quotes in fields

  • embedded newlines in fields

Examples of programs that produce such beasts include MySQL and Excel.

For a higher-level, friendlier CSV class with many conveniences, see DataTable (which uses this class for its parsing).

Example:

records = []
parse = CSVParser().parse
for line in lines:
    results = parse(line)
    if results is not None:
        records.append(results)

CREDIT

The algorithm was taken directly from the open source Python C-extension, csv: https://www.object-craft.com.au/projects/csv/

It would be nice to use the csv module when present, since it is substantially faster. Before that can be done, it needs to support allowComments and stripWhitespace, and pass the TestCSVParser.py test suite.

__init__(allowComments=True, stripWhitespace=True, fieldSep=',', autoReset=True, doubleQuote=True)

Create a new CSV parser.

allowComments:

If true (the default), then comment lines using the Python comment marker are allowed.

stripWhitespace:

If true (the default), then left and right whitespace is stripped off from all fields.

fieldSep:

Defines the field separator string (a comma by default).

autoReset:

If true (the default), recover from errors automatically.

doubleQuote:

If true (the default), assume quotes in fields are escaped by appearing doubled.

endQuotedField(c)
inField(c)
inQuotedField(c)
parse(line)

Parse a single line and return a list of string fields.

Returns None if the CSV record contains embedded newlines and the record is not yet complete.

quoteInField(c)
quoteInQuotedField(c)
reset()

Reset the parser.

Resets the parser to a fresh state in order to recover from exceptions. But if autoReset is true (the default), this is done automatically.

saveField()
startField(c)
startRecord(c)
exception MiscUtils.CSVParser.ParseError

Bases: Exception

CSV file parse error.

__init__(*args, **kwargs)
args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

MiscUtils.CSVParser.parse(line)

Parse a single line and return a list of string fields.

Returns None if the CSV record contains embedded newlines and the record is not yet complete.