PyDocs

File Formats

csv

CSV parsing and writing.

This module provides classes that assist in the reading and writing
of Comma Separated Value (CSV) files, and implements the interface
described by PEP 305.  Although many CSV files are simple to parse,
the format is not formally defined by a stable specification and
is subtle enough that parsing lines of a CSV file with something
like line.split(",") is bound to fail.  The module supports three
basic APIs: reading, writing, and registration of dialects.


DIALECT REGISTRATION:

Readers and writers support a dialect argument, which is a convenient
handle on a group of settings.  When the dialect argument is a string,
it identifies one of the dialects previously registered with the module.
If it is a class or instance, the attributes of the argument are used as
the settings for the reader or writer:

    class excel:
        delimiter = ','
        quotechar = '"'
        escapechar = None
        doublequote = True
        skipinitialspace = False
        lineterminator = '\r\n'
        quoting = QUOTE_MINIMAL

SETTINGS:

    * quotechar - specifies a one-character string to use as the
        quoting character.  It defaults to '"'.
    * delimiter - specifies a one-character string to use as the
        field separator.  It defaults to ','.
    * skipinitialspace - specifies how to interpret spaces which
        immediately follow a delimiter.  It defaults to False, which
        means that spaces immediately following a delimiter is part
        of the following field.
    * lineterminator - specifies the character sequence which should
        terminate rows.
    * quoting - controls when quotes should be generated by the writer.
        It can take on any of the following module constants:

        csv.QUOTE_MINIMAL means only when required, for example, when a
            field contains either the quotechar or the delimiter
        csv.QUOTE_ALL means that quotes are always placed around fields.
        csv.QUOTE_NONNUMERIC means that quotes are always placed around
            fields which do not parse as integers or floating-point
            numbers.
        csv.QUOTE_STRINGS means that quotes are always placed around
            fields which are strings.  Note that the Python value None
            is not a string.
        csv.QUOTE_NOTNULL means that quotes are only placed around fields
            that are not the Python value None.
        csv.QUOTE_NONE means that quotes are never placed around fields.
    * escapechar - specifies a one-character string used to escape
        the delimiter when quoting is set to QUOTE_NONE.
    * doublequote - controls the handling of quotes inside fields.  When
        True, two consecutive quotes are interpreted as one during read,
        and when writing, each quote character embedded in the data is
        written as two quotes
Symbols in csv

c

csvreader.__next__
Return the next row of the reader’s iterable object as a list (if the object was returned from reader()) or a dict (if it is a DictReader instance), parsed according to the current Dialect.
csvreader.dialect
A read-only description of the dialect in use by the parser.
csvreader.line_num
The number of lines read from the source iterator.
csvwriter.dialect
A read-only description of the dialect in use by the writer.
csvwriter.writerow
Write the row parameter to the writer’s file object, formatted according to the current Dialect.
csvwriter.writerows
Write all elements in rows (an iterable of row objects as described above) to the writer’s file object, formatted according to the current dialect.

d

Dialect
Describe a CSV dialect.
DictReader
Create an object that operates like a regular reader but maps the information in each row to a dict whose keys are given by the optional fieldnames parameter.
DictWriter
Create an object which operates like a regular writer but maps dictionaries onto output rows.

e

Error
Raised by any of the functions when an error is detected.
excel
Describe the usual properties of Excel-generated CSV files.
excel_tab
Describe the usual properties of Excel-generated TAB-delimited files.

f

field_size_limit
Sets an upper limit on parsed fields.

g

get_dialect
Return the dialect instance associated with name.

l

list_dialects
Return a list of all known dialect names.

q

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, '\r', '\n' or any of the characters in lineterminator.
QUOTE_NONE
Instructs writer objects to never quote fields.
QUOTE_NONNUMERIC
Instructs writer objects to quote all non-numeric fields.
QUOTE_NOTNULL
Instructs writer objects to quote all fields which are not None.
QUOTE_STRINGS
Instructs writer objects to always place quotes around fields which are strings.

r

reader
Return a reader object that will process lines from the given iterable.
register_dialect
Create a mapping from a string name to a CVS dialect.

s

Sniffer
"Sniffs" the format of a CSV file (i.e.

u

unix_dialect
Describe the usual properties of Unix-generated CSV files.
unregister_dialect
Delete the name/dialect mapping associated with a string name.

w

writer
Return a writer object that will write user data on the given file object.