MiscUtils.CSVParser
index
/data/Webware-Aleman/MiscUtils/CSVParser.py

CSVParser.py
 
A parser for CSV files.

 
Modules
       
types

 
Classes
       
CSVParser
exceptions.Exception(exceptions.BaseException)
ParseError

 
class CSVParser
    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:
        http://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.
 
  Methods defined here:
__init__(self, allowComments=1, stripWhitespace=1, fieldSep=',', autoReset=1, doubleQuote=1)
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(self, c)
inField(self, c)
inQuotedField(self, c)
parse(self, 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(self, c)
quoteInQuotedField(self, c)
reset(self)
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(self)
startField(self, c)
startRecord(self, c)

 
class ParseError(exceptions.Exception)
    
Method resolution order:
ParseError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
Functions
       
joinCSVFields(fields)
Return a CSV record (e.g. a string) from a sequence of fields.
 
Fields containing commands (,) or double quotes (") are quoted and
double quotes are escaped (""). The terminating newline is NOT included.
parse(self, line) method of CSVParser instance
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.

 
Data
        EndQuotedField = 6
Finished = 10
InField = 2
InQuotedField = 4
QuoteInField = 3
QuoteInQuotedField = 5
StartField = 1
StartRecord = 0