12.20.1 Module Contents
The csv module defines the following functions:
reader( |
csvfile[,
dialect='excel' [, fmtparam]]) |
-
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. 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 12.20.2, ``Dialects and Formatting
Parameters'' for details of these parameters.
All data read are returned as strings. No automatic data type
conversion is performed.
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 12.20.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) |
-
Associate dialect with name. dialect must be a subclass
of csv.Dialect. name must be a string or Unicode object.
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.
-
Return the dialect associated with name. An Error is
raised if name is not a registered dialect name.
-
Return the names of all registered dialects.
The csv module defines the following classes:
class DictReader( |
csvfile[,
fieldnames=None,[,
restkey=None[,
restval=None[,
dialect='excel' [,
*args, **kwds]]]]]) |
-
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.
class DictWriter( |
csvfile, fieldnames[,
restval=""[,
extrasaction='raise' [,
dialect='excel' [,
*args, **kwds]]]]) |
-
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.
- class Dialect
-
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.
-
The Sniffer class is used to deduce the format of a CSV file.
The Sniffer class provides a single method:
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.
-
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
the current delimiter or begin with the current quotechar.
- QUOTE_NONNUMERIC
-
Instructs writer objects to quote all non-numeric fields.
- 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. When QUOTE_NONE is in effect, it
is an error not to have a single-character escapechar defined, even if
no data to be written contains the delimiter character.
The csv module defines the following exception:
- exception Error
-
Raised by any of the functions when an error is detected.
Release 2.4.2, documentation updated on 28 September 2005.
See About this document... for information on suggesting changes.