|
1 | 1 |
|
2 |
| -""" |
3 |
| -csv.py - read/write/investigate CSV files |
| 2 | +r""" |
| 3 | +CSV parsing and writing. |
| 4 | +
|
| 5 | +This module provides classes that assist in the reading and writing |
| 6 | +of Comma Separated Value (CSV) files, and implements the interface |
| 7 | +described by PEP 305. Although many CSV files are simple to parse, |
| 8 | +the format is not formally defined by a stable specification and |
| 9 | +is subtle enough that parsing lines of a CSV file with something |
| 10 | +like line.split(",") is bound to fail. The module supports three |
| 11 | +basic APIs: reading, writing, and registration of dialects. |
| 12 | +
|
| 13 | +
|
| 14 | +DIALECT REGISTRATION: |
| 15 | +
|
| 16 | +Readers and writers support a dialect argument, which is a convenient |
| 17 | +handle on a group of settings. When the dialect argument is a string, |
| 18 | +it identifies one of the dialects previously registered with the module. |
| 19 | +If it is a class or instance, the attributes of the argument are used as |
| 20 | +the settings for the reader or writer: |
| 21 | +
|
| 22 | + class excel: |
| 23 | + delimiter = ',' |
| 24 | + quotechar = '"' |
| 25 | + escapechar = None |
| 26 | + doublequote = True |
| 27 | + skipinitialspace = False |
| 28 | + lineterminator = '\r\n' |
| 29 | + quoting = QUOTE_MINIMAL |
| 30 | +
|
| 31 | +SETTINGS: |
| 32 | +
|
| 33 | + * quotechar - specifies a one-character string to use as the |
| 34 | + quoting character. It defaults to '"'. |
| 35 | + * delimiter - specifies a one-character string to use as the |
| 36 | + field separator. It defaults to ','. |
| 37 | + * skipinitialspace - specifies how to interpret spaces which |
| 38 | + immediately follow a delimiter. It defaults to False, which |
| 39 | + means that spaces immediately following a delimiter is part |
| 40 | + of the following field. |
| 41 | + * lineterminator - specifies the character sequence which should |
| 42 | + terminate rows. |
| 43 | + * quoting - controls when quotes should be generated by the writer. |
| 44 | + It can take on any of the following module constants: |
| 45 | +
|
| 46 | + csv.QUOTE_MINIMAL means only when required, for example, when a |
| 47 | + field contains either the quotechar or the delimiter |
| 48 | + csv.QUOTE_ALL means that quotes are always placed around fields. |
| 49 | + csv.QUOTE_NONNUMERIC means that quotes are always placed around |
| 50 | + fields which do not parse as integers or floating point |
| 51 | + numbers. |
| 52 | + csv.QUOTE_STRINGS means that quotes are always placed around |
| 53 | + fields which are strings. Note that the Python value None |
| 54 | + is not a string. |
| 55 | + csv.QUOTE_NOTNULL means that quotes are only placed around fields |
| 56 | + that are not the Python value None. |
| 57 | + csv.QUOTE_NONE means that quotes are never placed around fields. |
| 58 | + * escapechar - specifies a one-character string used to escape |
| 59 | + the delimiter when quoting is set to QUOTE_NONE. |
| 60 | + * doublequote - controls the handling of quotes inside fields. When |
| 61 | + True, two consecutive quotes are interpreted as one during read, |
| 62 | + and when writing, each quote character embedded in the data is |
| 63 | + written as two quotes |
4 | 64 | """
|
5 | 65 |
|
6 | 66 | import re
|
7 | 67 | import types
|
8 |
| -from _csv import Error, __version__, writer, reader, register_dialect, \ |
| 68 | +from _csv import Error, writer, reader, register_dialect, \ |
9 | 69 | unregister_dialect, get_dialect, list_dialects, \
|
10 | 70 | field_size_limit, \
|
11 | 71 | QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \
|
12 |
| - QUOTE_STRINGS, QUOTE_NOTNULL, \ |
13 |
| - __doc__ |
| 72 | + QUOTE_STRINGS, QUOTE_NOTNULL |
14 | 73 | from _csv import Dialect as _Dialect
|
15 | 74 |
|
16 | 75 | from io import StringIO
|
17 | 76 |
|
18 | 77 | __all__ = ["QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
|
19 | 78 | "QUOTE_STRINGS", "QUOTE_NOTNULL",
|
20 |
| - "Error", "Dialect", "__doc__", "excel", "excel_tab", |
| 79 | + "Error", "Dialect", "excel", "excel_tab", |
21 | 80 | "field_size_limit", "reader", "writer",
|
22 | 81 | "register_dialect", "get_dialect", "list_dialects", "Sniffer",
|
23 |
| - "unregister_dialect", "__version__", "DictReader", "DictWriter", |
| 82 | + "unregister_dialect", "DictReader", "DictWriter", |
24 | 83 | "unix_dialect"]
|
25 | 84 |
|
| 85 | +__version__ = "1.0" |
| 86 | + |
| 87 | + |
26 | 88 | class Dialect:
|
27 | 89 | """Describe a CSV dialect.
|
28 | 90 |
|
|
0 commit comments