MiscUtils.CSVParser
index
/usr/local/share/webware/MiscUtils/CSVParser.py

 
Modules
       
types

 
Classes
       
CSVParser
exceptions.Exception(exceptions.BaseException)
ParseError

 
class CSVParser
    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)
@@ document
endQuotedField(self, c)
inField(self, c)
inQuotedField(self, c)
parse(self, line)
Parse the single line and return a list or string fields, or
None if the CSV record contains embedded newlines and the
record is not yet complete.
quoteInField(self, c)
quoteInQuotedField(self, c)
reset(self)
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 x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x5d3320>
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)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
Functions
       
joinCSVFields(fields)
Returns a CSV record (eg 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 the single line and return a list or string fields, or
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