Return a reader object which will iterate over lines in the given
csvfile. csvfile can be any object which supports the
iterator protocol and returns a string each time its next
method is called -- file objects and list objects are both suitable.
If csvfile is a file object, it must be opened with
the 'b' flag on platforms where that makes a difference. An optional
dialect parameter can be given
which is used to define a set of parameters specific to a particular CSV
dialect. It may be an instance of a subclass of the Dialect
class or one of the strings returned by the list_dialects
function. The other optional fmtparam keyword arguments can be
given to override individual formatting parameters in the current
dialect. For more information about the dialect and formatting
parameters, see section 9.1.2, ``Dialects and Formatting
Parameters'' for details of these parameters.
All data read are returned as strings. No automatic data type
conversion is performed.
Changed in version 2.5:
The parser is now stricter with respect to multi-line quoted
fields. Previously, if a line ended within a quoted field without a
terminating newline character, a newline would be inserted into the
returned field. This behavior caused problems when reading files
which contained carriage return characters within fields. The
behavior was changed to return the field without inserting newlines. As
a consequence, if newlines embedded within fields are important, the
input should be split into lines in a manner which preserves the newline
characters.
writer(
csvfile[,
dialect='excel'][, fmtparam])
Return a writer object responsible for converting the user's data into
delimited strings on the given file-like object. csvfile can be any
object with a write method. If csvfile is a file object,
it must be opened with the 'b' flag on platforms where that makes a
difference. An optional
dialect parameter can be given which is used to define a set of
parameters specific to a particular CSV dialect. It may be an instance
of a subclass of the Dialect class or one of the strings
returned by the list_dialects function. The other optional
fmtparam keyword arguments can be given to override individual
formatting parameters in the current dialect. For more information
about the dialect and formatting parameters, see
section 9.1.2, ``Dialects and Formatting Parameters'' for
details of these parameters. To make it as easy as possible to
interface with modules which implement the DB API, the value
None is written as the empty string. While this isn't a
reversible transformation, it makes it easier to dump SQL NULL data values
to CSV files without preprocessing the data returned from a
cursor.fetch*() call. All other non-string data are stringified
with str() before being written.
register_dialect(
name[, dialect][, fmtparam])
Associate dialect with name. name must be a string
or Unicode object. The dialect can be specified either by passing a
sub-class of Dialect, or by fmtparam keyword arguments,
or both, with keyword arguments overriding parameters of the dialect.
For more information about the dialect and formatting parameters, see
section 9.1.2, ``Dialects and Formatting Parameters''
for details of these parameters.
unregister_dialect(
name)
Delete the dialect associated with name from the dialect registry. An
Error is raised if name is not a registered dialect
name.
get_dialect(
name)
Return the dialect associated with name. An Error is
raised if name is not a registered dialect name.
list_dialects(
)
Return the names of all registered dialects.
field_size_limit(
[new_limit])
Returns the current maximum field size allowed by the parser. If
new_limit is given, this becomes the new limit.
New in version 2.5.
Create an object which operates like a regular reader but maps the
information read into a dict whose keys are given by the optional
fieldnames
parameter. If the fieldnames parameter is omitted, the values in
the first row of the csvfile will be used as the fieldnames.
If the row read has fewer fields than the fieldnames sequence,
the value of restval will be used as the default value. If the row
read has more fields than the fieldnames sequence, the remaining data is
added as a sequence keyed by the value of restkey. If the row read
has fewer fields than the fieldnames sequence, the remaining keys take the
value of the optional restval parameter. Any other optional or
keyword arguments are passed to the underlying reader instance.
Create an object which operates like a regular writer but maps dictionaries
onto output rows. The fieldnames parameter identifies the order in
which values in the dictionary passed to the writerow() method are
written to the csvfile. The optional restval parameter
specifies the value to be written if the dictionary is missing a key in
fieldnames. If the dictionary passed to the writerow()
method contains a key not found in fieldnames, the optional
extrasaction parameter indicates what action to take. If it is set
to 'raise' a ValueError is raised. If it is set to
'ignore', extra values in the dictionary are ignored. Any other
optional or keyword arguments are passed to the underlying writer
instance.
Note that unlike the DictReader class, the fieldnames
parameter of the DictWriter is not optional. Since Python's
dict objects are not ordered, there is not enough information
available to deduce the order in which the row should be written to the
csvfile.
classDialect
The Dialect class is a container class relied on primarily for its
attributes, which are used to define the parameters for a specific
reader or writer instance.
classexcel(
)
The excel class defines the usual properties of an Excel-generated
CSV file.
classexcel_tab(
)
The excel_tab class defines the usual properties of an
Excel-generated TAB-delimited file.
classSniffer(
)
The Sniffer class is used to deduce the format of a CSV file.
The Sniffer class provides two methods:
sniff(
sample[,delimiters=None])
Analyze the given sample and return a Dialect subclass
reflecting the parameters found. If the optional delimiters parameter
is given, it is interpreted as a string containing possible valid delimiter
characters.
has_header(
sample)
Analyze the sample text (presumed to be in CSV format) and return
True if the first row appears to be a series of column
headers.
The csv module defines the following constants:
QUOTE_ALL
Instructs writer objects to quote all fields.
QUOTE_MINIMAL
Instructs writer objects to only quote those fields which contain
special characters such as delimiter, quotechar or any of the
characters in lineterminator.
QUOTE_NONNUMERIC
Instructs writer objects to quote all non-numeric
fields.
Instructs the reader to convert all non-quoted fields to type float.
QUOTE_NONE
Instructs writer objects to never quote fields. When the current
delimiter occurs in output data it is preceded by the current
escapechar character. If escapechar is not set, the writer
will raise Error if any characters that require escaping
are encountered.
Instructs reader to perform no special processing of quote characters.
The csv module defines the following exception:
exceptionError
Raised by any of the functions when an error is detected.