1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
CSV Documentation
(html version available at http://object-craft.com.au/projects/csv/)
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:
* If the closing " on a quoted field is not immediately followed by either end of
line, or a field separator.
* If an end of line is encountered which is not at the end of string
|