Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`csv` --- CSV File Reading and Writing |
| 2 | =========================================== |
| 3 | |
| 4 | .. module:: csv |
| 5 | :synopsis: Write and read tabular data to and from delimited files. |
| 6 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
| 7 | |
Andrew Kuchling | 2e3743c | 2014-03-19 16:23:01 -0400 | [diff] [blame] | 8 | **Source code:** :source:`Lib/csv.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | .. index:: |
| 11 | single: csv |
| 12 | pair: data; tabular |
| 13 | |
| 14 | The so-called CSV (Comma Separated Values) format is the most common import and |
Skip Montanaro | b40dea7 | 2011-03-19 09:09:30 -0500 | [diff] [blame] | 15 | export format for spreadsheets and databases. CSV format was used for many |
| 16 | years prior to attempts to describe the format in a standardized way in |
| 17 | :rfc:`4180`. The lack of a well-defined standard means that subtle differences |
| 18 | often exist in the data produced and consumed by different applications. These |
| 19 | differences can make it annoying to process CSV files from multiple sources. |
| 20 | Still, while the delimiters and quoting characters vary, the overall format is |
| 21 | similar enough that it is possible to write a single module which can |
| 22 | efficiently manipulate such data, hiding the details of reading and writing the |
| 23 | data from the programmer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
| 25 | The :mod:`csv` module implements classes to read and write tabular data in CSV |
| 26 | format. It allows programmers to say, "write this data in the format preferred |
| 27 | by Excel," or "read data from this file which was generated by Excel," without |
| 28 | knowing the precise details of the CSV format used by Excel. Programmers can |
| 29 | also describe the CSV formats understood by other applications or define their |
| 30 | own special-purpose CSV formats. |
| 31 | |
| 32 | The :mod:`csv` module's :class:`reader` and :class:`writer` objects read and |
| 33 | write sequences. Programmers can also read and write data in dictionary form |
| 34 | using the :class:`DictReader` and :class:`DictWriter` classes. |
| 35 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | .. seealso:: |
| 37 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | :pep:`305` - CSV File API |
| 39 | The Python Enhancement Proposal which proposed this addition to Python. |
| 40 | |
| 41 | |
| 42 | .. _csv-contents: |
| 43 | |
| 44 | Module Contents |
| 45 | --------------- |
| 46 | |
| 47 | The :mod:`csv` module defines the following functions: |
| 48 | |
| 49 | |
R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 50 | .. index:: |
| 51 | single: universal newlines; csv.reader function |
| 52 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 53 | .. function:: reader(csvfile, dialect='excel', **fmtparams) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | Return a reader object which will iterate over lines in the given *csvfile*. |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 56 | *csvfile* can be any object which supports the :term:`iterator` protocol and returns a |
Georg Brandl | b30f330 | 2011-01-06 09:23:56 +0000 | [diff] [blame] | 57 | string each time its :meth:`!__next__` method is called --- :term:`file objects |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 58 | <file object>` and list objects are both suitable. If *csvfile* is a file object, |
R David Murray | 9188702 | 2011-03-19 22:30:14 -0400 | [diff] [blame] | 59 | it should be opened with ``newline=''``. [1]_ An optional |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | *dialect* parameter can be given which is used to define a set of parameters |
| 61 | specific to a particular CSV dialect. It may be an instance of a subclass of |
| 62 | the :class:`Dialect` class or one of the strings returned by the |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 63 | :func:`list_dialects` function. The other optional *fmtparams* keyword arguments |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | can be given to override individual formatting parameters in the current |
| 65 | dialect. For full details about the dialect and formatting parameters, see |
| 66 | section :ref:`csv-fmt-params`. |
| 67 | |
Skip Montanaro | 0468df3 | 2009-03-25 00:52:18 +0000 | [diff] [blame] | 68 | Each row read from the csv file is returned as a list of strings. No |
R. David Murray | 8b7d4aa | 2009-04-04 01:38:38 +0000 | [diff] [blame] | 69 | automatic data type conversion is performed unless the ``QUOTE_NONNUMERIC`` format |
| 70 | option is specified (in which case unquoted fields are transformed into floats). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 72 | A short usage example:: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 73 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 74 | >>> import csv |
Ezio Melotti | e34f8a9 | 2012-09-15 05:51:45 +0300 | [diff] [blame] | 75 | >>> with open('eggs.csv', newline='') as csvfile: |
| 76 | ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') |
| 77 | ... for row in spamreader: |
| 78 | ... print(', '.join(row)) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 79 | Spam, Spam, Spam, Spam, Spam, Baked Beans |
| 80 | Spam, Lovely Spam, Wonderful Spam |
| 81 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 83 | .. function:: writer(csvfile, dialect='excel', **fmtparams) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
| 85 | Return a writer object responsible for converting the user's data into delimited |
| 86 | strings on the given file-like object. *csvfile* can be any object with a |
R David Murray | 9c0d5ea | 2011-03-20 11:18:21 -0400 | [diff] [blame] | 87 | :func:`write` method. If *csvfile* is a file object, it should be opened with |
| 88 | ``newline=''`` [1]_. An optional *dialect* |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | parameter can be given which is used to define a set of parameters specific to a |
| 90 | particular CSV dialect. It may be an instance of a subclass of the |
| 91 | :class:`Dialect` class or one of the strings returned by the |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 92 | :func:`list_dialects` function. The other optional *fmtparams* keyword arguments |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | can be given to override individual formatting parameters in the current |
| 94 | dialect. For full details about the dialect and formatting parameters, see |
| 95 | section :ref:`csv-fmt-params`. To make it |
| 96 | as easy as possible to interface with modules which implement the DB API, the |
| 97 | value :const:`None` is written as the empty string. While this isn't a |
| 98 | reversible transformation, it makes it easier to dump SQL NULL data values to |
| 99 | CSV files without preprocessing the data returned from a ``cursor.fetch*`` call. |
| 100 | All other non-string data are stringified with :func:`str` before being written. |
| 101 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 102 | A short usage example:: |
| 103 | |
Ezio Melotti | e34f8a9 | 2012-09-15 05:51:45 +0300 | [diff] [blame] | 104 | import csv |
| 105 | with open('eggs.csv', 'w', newline='') as csvfile: |
| 106 | spamwriter = csv.writer(csvfile, delimiter=' ', |
| 107 | quotechar='|', quoting=csv.QUOTE_MINIMAL) |
| 108 | spamwriter.writerow(['Spam'] * 5 + ['Baked Beans']) |
| 109 | spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 110 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Berker Peksag | 12b50ce | 2015-06-05 15:17:51 +0300 | [diff] [blame] | 112 | .. function:: register_dialect(name[, dialect[, **fmtparams]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 114 | Associate *dialect* with *name*. *name* must be a string. The |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | dialect can be specified either by passing a sub-class of :class:`Dialect`, or |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 116 | by *fmtparams* keyword arguments, or both, with keyword arguments overriding |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | parameters of the dialect. For full details about the dialect and formatting |
| 118 | parameters, see section :ref:`csv-fmt-params`. |
| 119 | |
| 120 | |
| 121 | .. function:: unregister_dialect(name) |
| 122 | |
| 123 | Delete the dialect associated with *name* from the dialect registry. An |
| 124 | :exc:`Error` is raised if *name* is not a registered dialect name. |
| 125 | |
| 126 | |
| 127 | .. function:: get_dialect(name) |
| 128 | |
Georg Brandl | 6554cb9 | 2007-12-02 23:15:43 +0000 | [diff] [blame] | 129 | Return the dialect associated with *name*. An :exc:`Error` is raised if |
| 130 | *name* is not a registered dialect name. This function returns an immutable |
| 131 | :class:`Dialect`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 | |
| 133 | .. function:: list_dialects() |
| 134 | |
| 135 | Return the names of all registered dialects. |
| 136 | |
| 137 | |
| 138 | .. function:: field_size_limit([new_limit]) |
| 139 | |
| 140 | Returns the current maximum field size allowed by the parser. If *new_limit* is |
| 141 | given, this becomes the new limit. |
| 142 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | The :mod:`csv` module defines the following classes: |
| 145 | |
R David Murray | f031a6f | 2014-02-24 15:32:54 -0500 | [diff] [blame] | 146 | .. class:: DictReader(csvfile, fieldnames=None, restkey=None, restval=None, \ |
| 147 | dialect='excel', *args, **kwds) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
R David Murray | f031a6f | 2014-02-24 15:32:54 -0500 | [diff] [blame] | 149 | Create an object which operates like a regular reader but maps the |
| 150 | information read into a dict whose keys are given by the optional |
| 151 | *fieldnames* parameter. The *fieldnames* parameter is a :mod:`sequence |
| 152 | <collections.abc>` whose elements are associated with the fields of the |
| 153 | input data in order. These elements become the keys of the resulting |
| 154 | dictionary. If the *fieldnames* parameter is omitted, the values in the |
| 155 | first row of the *csvfile* will be used as the fieldnames. If the row read |
| 156 | has more fields than the fieldnames sequence, the remaining data is added as |
| 157 | a sequence keyed by the value of *restkey*. If the row read has fewer |
| 158 | fields than the fieldnames sequence, the remaining keys take the value of |
| 159 | the optional *restval* parameter. Any other optional or keyword arguments |
| 160 | are passed to the underlying :class:`reader` instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
Berker Peksag | c8c64e3 | 2014-11-24 23:46:30 +0200 | [diff] [blame] | 162 | A short usage example:: |
| 163 | |
| 164 | >>> import csv |
| 165 | >>> with open('names.csv') as csvfile: |
| 166 | ... reader = csv.DictReader(csvfile) |
| 167 | ... for row in reader: |
| 168 | ... print(row['first_name'], row['last_name']) |
| 169 | ... |
| 170 | Baked Beans |
| 171 | Lovely Spam |
| 172 | Wonderful Spam |
| 173 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
R David Murray | f031a6f | 2014-02-24 15:32:54 -0500 | [diff] [blame] | 175 | .. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', \ |
| 176 | dialect='excel', *args, **kwds) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
R David Murray | f031a6f | 2014-02-24 15:32:54 -0500 | [diff] [blame] | 178 | Create an object which operates like a regular writer but maps dictionaries |
| 179 | onto output rows. The *fieldnames* parameter is a :mod:`sequence |
| 180 | <collections.abc>` of keys that identify the order in which values in the |
| 181 | dictionary passed to the :meth:`writerow` method are written to the |
| 182 | *csvfile*. The optional *restval* parameter specifies the value to be |
| 183 | written if the dictionary is missing a key in *fieldnames*. If the |
| 184 | dictionary passed to the :meth:`writerow` method contains a key not found in |
| 185 | *fieldnames*, the optional *extrasaction* parameter indicates what action to |
| 186 | take. If it is set to ``'raise'`` a :exc:`ValueError` is raised. If it is |
| 187 | set to ``'ignore'``, extra values in the dictionary are ignored. Any other |
| 188 | optional or keyword arguments are passed to the underlying :class:`writer` |
| 189 | instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
R David Murray | f031a6f | 2014-02-24 15:32:54 -0500 | [diff] [blame] | 191 | Note that unlike the :class:`DictReader` class, the *fieldnames* parameter |
| 192 | of the :class:`DictWriter` is not optional. Since Python's :class:`dict` |
| 193 | objects are not ordered, there is not enough information available to deduce |
| 194 | the order in which the row should be written to the *csvfile*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | |
Berker Peksag | c8c64e3 | 2014-11-24 23:46:30 +0200 | [diff] [blame] | 196 | A short usage example:: |
| 197 | |
| 198 | import csv |
| 199 | |
| 200 | with open('names.csv', 'w') as csvfile: |
| 201 | fieldnames = ['first_name', 'last_name'] |
| 202 | writer = csv.DictWriter(csvfile, fieldnames=fieldnames) |
| 203 | |
| 204 | writer.writeheader() |
| 205 | writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'}) |
| 206 | writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'}) |
| 207 | writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'}) |
| 208 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
| 210 | .. class:: Dialect |
| 211 | |
| 212 | The :class:`Dialect` class is a container class relied on primarily for its |
| 213 | attributes, which are used to define the parameters for a specific |
| 214 | :class:`reader` or :class:`writer` instance. |
| 215 | |
| 216 | |
| 217 | .. class:: excel() |
| 218 | |
| 219 | The :class:`excel` class defines the usual properties of an Excel-generated CSV |
| 220 | file. It is registered with the dialect name ``'excel'``. |
| 221 | |
| 222 | |
| 223 | .. class:: excel_tab() |
| 224 | |
| 225 | The :class:`excel_tab` class defines the usual properties of an Excel-generated |
| 226 | TAB-delimited file. It is registered with the dialect name ``'excel-tab'``. |
| 227 | |
| 228 | |
Georg Brandl | 7424dd3 | 2010-10-27 07:27:06 +0000 | [diff] [blame] | 229 | .. class:: unix_dialect() |
| 230 | |
| 231 | The :class:`unix_dialect` class defines the usual properties of a CSV file |
| 232 | generated on UNIX systems, i.e. using ``'\n'`` as line terminator and quoting |
| 233 | all fields. It is registered with the dialect name ``'unix'``. |
| 234 | |
| 235 | .. versionadded:: 3.2 |
| 236 | |
| 237 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | .. class:: Sniffer() |
| 239 | |
| 240 | The :class:`Sniffer` class is used to deduce the format of a CSV file. |
| 241 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 242 | The :class:`Sniffer` class provides two methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 244 | .. method:: sniff(sample, delimiters=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 246 | Analyze the given *sample* and return a :class:`Dialect` subclass |
| 247 | reflecting the parameters found. If the optional *delimiters* parameter |
| 248 | is given, it is interpreted as a string containing possible valid |
| 249 | delimiter characters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
| 251 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 252 | .. method:: has_header(sample) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 254 | Analyze the sample text (presumed to be in CSV format) and return |
| 255 | :const:`True` if the first row appears to be a series of column headers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 256 | |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 257 | An example for :class:`Sniffer` use:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 258 | |
Ezio Melotti | e34f8a9 | 2012-09-15 05:51:45 +0300 | [diff] [blame] | 259 | with open('example.csv') as csvfile: |
| 260 | dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
| 261 | csvfile.seek(0) |
| 262 | reader = csv.reader(csvfile, dialect) |
| 263 | # ... process CSV file contents here ... |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 264 | |
| 265 | |
| 266 | The :mod:`csv` module defines the following constants: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | .. data:: QUOTE_ALL |
| 269 | |
| 270 | Instructs :class:`writer` objects to quote all fields. |
| 271 | |
| 272 | |
| 273 | .. data:: QUOTE_MINIMAL |
| 274 | |
| 275 | Instructs :class:`writer` objects to only quote those fields which contain |
| 276 | special characters such as *delimiter*, *quotechar* or any of the characters in |
| 277 | *lineterminator*. |
| 278 | |
| 279 | |
| 280 | .. data:: QUOTE_NONNUMERIC |
| 281 | |
| 282 | Instructs :class:`writer` objects to quote all non-numeric fields. |
| 283 | |
| 284 | Instructs the reader to convert all non-quoted fields to type *float*. |
| 285 | |
| 286 | |
| 287 | .. data:: QUOTE_NONE |
| 288 | |
| 289 | Instructs :class:`writer` objects to never quote fields. When the current |
| 290 | *delimiter* occurs in output data it is preceded by the current *escapechar* |
| 291 | character. If *escapechar* is not set, the writer will raise :exc:`Error` if |
| 292 | any characters that require escaping are encountered. |
| 293 | |
| 294 | Instructs :class:`reader` to perform no special processing of quote characters. |
| 295 | |
| 296 | The :mod:`csv` module defines the following exception: |
| 297 | |
| 298 | |
| 299 | .. exception:: Error |
| 300 | |
| 301 | Raised by any of the functions when an error is detected. |
| 302 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | .. _csv-fmt-params: |
| 304 | |
| 305 | Dialects and Formatting Parameters |
| 306 | ---------------------------------- |
| 307 | |
| 308 | To make it easier to specify the format of input and output records, specific |
| 309 | formatting parameters are grouped together into dialects. A dialect is a |
| 310 | subclass of the :class:`Dialect` class having a set of specific methods and a |
| 311 | single :meth:`validate` method. When creating :class:`reader` or |
| 312 | :class:`writer` objects, the programmer can specify a string or a subclass of |
| 313 | the :class:`Dialect` class as the dialect parameter. In addition to, or instead |
| 314 | of, the *dialect* parameter, the programmer can also specify individual |
| 315 | formatting parameters, which have the same names as the attributes defined below |
| 316 | for the :class:`Dialect` class. |
| 317 | |
| 318 | Dialects support the following attributes: |
| 319 | |
| 320 | |
| 321 | .. attribute:: Dialect.delimiter |
| 322 | |
| 323 | A one-character string used to separate fields. It defaults to ``','``. |
| 324 | |
| 325 | |
| 326 | .. attribute:: Dialect.doublequote |
| 327 | |
| 328 | Controls how instances of *quotechar* appearing inside a field should be |
| 329 | themselves be quoted. When :const:`True`, the character is doubled. When |
| 330 | :const:`False`, the *escapechar* is used as a prefix to the *quotechar*. It |
| 331 | defaults to :const:`True`. |
| 332 | |
| 333 | On output, if *doublequote* is :const:`False` and no *escapechar* is set, |
| 334 | :exc:`Error` is raised if a *quotechar* is found in a field. |
| 335 | |
| 336 | |
| 337 | .. attribute:: Dialect.escapechar |
| 338 | |
| 339 | A one-character string used by the writer to escape the *delimiter* if *quoting* |
| 340 | is set to :const:`QUOTE_NONE` and the *quotechar* if *doublequote* is |
| 341 | :const:`False`. On reading, the *escapechar* removes any special meaning from |
| 342 | the following character. It defaults to :const:`None`, which disables escaping. |
| 343 | |
| 344 | |
| 345 | .. attribute:: Dialect.lineterminator |
| 346 | |
| 347 | The string used to terminate lines produced by the :class:`writer`. It defaults |
| 348 | to ``'\r\n'``. |
| 349 | |
| 350 | .. note:: |
| 351 | |
| 352 | The :class:`reader` is hard-coded to recognise either ``'\r'`` or ``'\n'`` as |
| 353 | end-of-line, and ignores *lineterminator*. This behavior may change in the |
| 354 | future. |
| 355 | |
| 356 | |
| 357 | .. attribute:: Dialect.quotechar |
| 358 | |
| 359 | A one-character string used to quote fields containing special characters, such |
| 360 | as the *delimiter* or *quotechar*, or which contain new-line characters. It |
| 361 | defaults to ``'"'``. |
| 362 | |
| 363 | |
| 364 | .. attribute:: Dialect.quoting |
| 365 | |
| 366 | Controls when quotes should be generated by the writer and recognised by the |
| 367 | reader. It can take on any of the :const:`QUOTE_\*` constants (see section |
| 368 | :ref:`csv-contents`) and defaults to :const:`QUOTE_MINIMAL`. |
| 369 | |
| 370 | |
| 371 | .. attribute:: Dialect.skipinitialspace |
| 372 | |
| 373 | When :const:`True`, whitespace immediately following the *delimiter* is ignored. |
| 374 | The default is :const:`False`. |
| 375 | |
| 376 | |
Ezio Melotti | a69be28 | 2012-11-18 12:55:35 +0200 | [diff] [blame] | 377 | .. attribute:: Dialect.strict |
| 378 | |
| 379 | When ``True``, raise exception :exc:`Error` on bad CSV input. |
| 380 | The default is ``False``. |
| 381 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | Reader Objects |
| 383 | -------------- |
| 384 | |
| 385 | Reader objects (:class:`DictReader` instances and objects returned by the |
| 386 | :func:`reader` function) have the following public methods: |
| 387 | |
Georg Brandl | c748506 | 2009-04-01 15:53:15 +0000 | [diff] [blame] | 388 | .. method:: csvreader.__next__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
| 390 | Return the next row of the reader's iterable object as a list, parsed according |
Georg Brandl | c748506 | 2009-04-01 15:53:15 +0000 | [diff] [blame] | 391 | to the current dialect. Usually you should call this as ``next(reader)``. |
| 392 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | Reader objects have the following public attributes: |
| 395 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 396 | .. attribute:: csvreader.dialect |
| 397 | |
| 398 | A read-only description of the dialect in use by the parser. |
| 399 | |
| 400 | |
| 401 | .. attribute:: csvreader.line_num |
| 402 | |
| 403 | The number of lines read from the source iterator. This is not the same as the |
| 404 | number of records returned, as records can span multiple lines. |
| 405 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 406 | |
Skip Montanaro | af8fcfa | 2008-08-09 19:44:22 +0000 | [diff] [blame] | 407 | DictReader objects have the following public attribute: |
| 408 | |
Skip Montanaro | af8fcfa | 2008-08-09 19:44:22 +0000 | [diff] [blame] | 409 | .. attribute:: csvreader.fieldnames |
| 410 | |
| 411 | If not passed as a parameter when creating the object, this attribute is |
| 412 | initialized upon first access or when the first record is read from the |
| 413 | file. |
| 414 | |
Skip Montanaro | af8fcfa | 2008-08-09 19:44:22 +0000 | [diff] [blame] | 415 | |
| 416 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 417 | Writer Objects |
| 418 | -------------- |
| 419 | |
| 420 | :class:`Writer` objects (:class:`DictWriter` instances and objects returned by |
| 421 | the :func:`writer` function) have the following public methods. A *row* must be |
Serhiy Storchaka | 7901b48 | 2015-03-30 09:09:54 +0300 | [diff] [blame] | 422 | an iterable of strings or numbers for :class:`Writer` objects and a dictionary |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | mapping fieldnames to strings or numbers (by passing them through :func:`str` |
| 424 | first) for :class:`DictWriter` objects. Note that complex numbers are written |
| 425 | out surrounded by parens. This may cause some problems for other programs which |
| 426 | read CSV files (assuming they support complex numbers at all). |
| 427 | |
| 428 | |
| 429 | .. method:: csvwriter.writerow(row) |
| 430 | |
| 431 | Write the *row* parameter to the writer's file object, formatted according to |
| 432 | the current dialect. |
| 433 | |
Serhiy Storchaka | 7901b48 | 2015-03-30 09:09:54 +0300 | [diff] [blame] | 434 | .. versionchanged:: 3.5 |
| 435 | Added support of arbitrary iterables. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 436 | |
| 437 | .. method:: csvwriter.writerows(rows) |
| 438 | |
| 439 | Write all the *rows* parameters (a list of *row* objects as described above) to |
| 440 | the writer's file object, formatted according to the current dialect. |
| 441 | |
| 442 | Writer objects have the following public attribute: |
| 443 | |
| 444 | |
| 445 | .. attribute:: csvwriter.dialect |
| 446 | |
| 447 | A read-only description of the dialect in use by the writer. |
| 448 | |
| 449 | |
R. David Murray | be0698b | 2010-02-23 22:57:58 +0000 | [diff] [blame] | 450 | DictWriter objects have the following public method: |
| 451 | |
| 452 | |
| 453 | .. method:: DictWriter.writeheader() |
| 454 | |
| 455 | Write a row with the field names (as specified in the constructor). |
| 456 | |
R. David Murray | 19e4548 | 2010-02-23 23:00:34 +0000 | [diff] [blame] | 457 | .. versionadded:: 3.2 |
R. David Murray | be0698b | 2010-02-23 22:57:58 +0000 | [diff] [blame] | 458 | |
| 459 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 460 | .. _csv-examples: |
| 461 | |
| 462 | Examples |
| 463 | -------- |
| 464 | |
| 465 | The simplest example of reading a CSV file:: |
| 466 | |
| 467 | import csv |
Eli Bendersky | 6860a92 | 2011-03-11 15:47:36 +0200 | [diff] [blame] | 468 | with open('some.csv', newline='') as f: |
| 469 | reader = csv.reader(f) |
| 470 | for row in reader: |
| 471 | print(row) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 472 | |
| 473 | Reading a file with an alternate format:: |
| 474 | |
| 475 | import csv |
R David Murray | 9188702 | 2011-03-19 22:30:14 -0400 | [diff] [blame] | 476 | with open('passwd', newline='') as f: |
Eli Bendersky | 6860a92 | 2011-03-11 15:47:36 +0200 | [diff] [blame] | 477 | reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE) |
| 478 | for row in reader: |
| 479 | print(row) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 480 | |
| 481 | The corresponding simplest possible writing example is:: |
| 482 | |
| 483 | import csv |
R David Murray | 9188702 | 2011-03-19 22:30:14 -0400 | [diff] [blame] | 484 | with open('some.csv', 'w', newline='') as f: |
Eli Bendersky | 6860a92 | 2011-03-11 15:47:36 +0200 | [diff] [blame] | 485 | writer = csv.writer(f) |
| 486 | writer.writerows(someiterable) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 487 | |
R. David Murray | 8b7d4aa | 2009-04-04 01:38:38 +0000 | [diff] [blame] | 488 | Since :func:`open` is used to open a CSV file for reading, the file |
| 489 | will by default be decoded into unicode using the system default |
| 490 | encoding (see :func:`locale.getpreferredencoding`). To decode a file |
| 491 | using a different encoding, use the ``encoding`` argument of open:: |
| 492 | |
Eli Bendersky | 6860a92 | 2011-03-11 15:47:36 +0200 | [diff] [blame] | 493 | import csv |
| 494 | with open('some.csv', newline='', encoding='utf-8') as f: |
| 495 | reader = csv.reader(f) |
| 496 | for row in reader: |
| 497 | print(row) |
R. David Murray | 8b7d4aa | 2009-04-04 01:38:38 +0000 | [diff] [blame] | 498 | |
| 499 | The same applies to writing in something other than the system default |
| 500 | encoding: specify the encoding argument when opening the output file. |
| 501 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 502 | Registering a new dialect:: |
| 503 | |
| 504 | import csv |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 505 | csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE) |
R David Murray | 9188702 | 2011-03-19 22:30:14 -0400 | [diff] [blame] | 506 | with open('passwd', newline='') as f: |
Eli Bendersky | 6860a92 | 2011-03-11 15:47:36 +0200 | [diff] [blame] | 507 | reader = csv.reader(f, 'unixpwd') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | |
| 509 | A slightly more advanced use of the reader --- catching and reporting errors:: |
| 510 | |
| 511 | import csv, sys |
Eli Bendersky | 6860a92 | 2011-03-11 15:47:36 +0200 | [diff] [blame] | 512 | filename = 'some.csv' |
| 513 | with open(filename, newline='') as f: |
| 514 | reader = csv.reader(f) |
| 515 | try: |
| 516 | for row in reader: |
| 517 | print(row) |
| 518 | except csv.Error as e: |
| 519 | sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 520 | |
| 521 | And while the module doesn't directly support parsing strings, it can easily be |
| 522 | done:: |
| 523 | |
| 524 | import csv |
| 525 | for row in csv.reader(['one,two,three']): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 526 | print(row) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 527 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | |
R. David Murray | 8b7d4aa | 2009-04-04 01:38:38 +0000 | [diff] [blame] | 529 | .. rubric:: Footnotes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 530 | |
R David Murray | 9188702 | 2011-03-19 22:30:14 -0400 | [diff] [blame] | 531 | .. [1] If ``newline=''`` is not specified, newlines embedded inside quoted fields |
| 532 | will not be interpreted correctly, and on platforms that use ``\r\n`` linendings |
R David Murray | 9c0d5ea | 2011-03-20 11:18:21 -0400 | [diff] [blame] | 533 | on write an extra ``\r`` will be added. It should always be safe to specify |
R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 534 | ``newline=''``, since the csv module does its own |
| 535 | (:term:`universal <universal newlines>`) newline handling. |