blob: 61d39828e0194add42fac1e47fd1393c8a9d10fe [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`csv` --- CSV File Reading and Writing
2===========================================
3
4.. module:: csv
5 :synopsis: Write and read tabular data to and from delimited files.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
8
Andrew Kuchling2e3743c2014-03-19 16:23:01 -04009**Source code:** :source:`Lib/csv.py`
Georg Brandl116aa622007-08-15 14:28:22 +000010
Georg Brandl116aa622007-08-15 14:28:22 +000011.. index::
12 single: csv
13 pair: data; tabular
14
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040015--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +000017The so-called CSV (Comma Separated Values) format is the most common import and
Skip Montanarob40dea72011-03-19 09:09:30 -050018export format for spreadsheets and databases. CSV format was used for many
19years prior to attempts to describe the format in a standardized way in
20:rfc:`4180`. The lack of a well-defined standard means that subtle differences
21often exist in the data produced and consumed by different applications. These
22differences can make it annoying to process CSV files from multiple sources.
23Still, while the delimiters and quoting characters vary, the overall format is
24similar enough that it is possible to write a single module which can
25efficiently manipulate such data, hiding the details of reading and writing the
26data from the programmer.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28The :mod:`csv` module implements classes to read and write tabular data in CSV
29format. It allows programmers to say, "write this data in the format preferred
30by Excel," or "read data from this file which was generated by Excel," without
31knowing the precise details of the CSV format used by Excel. Programmers can
32also describe the CSV formats understood by other applications or define their
33own special-purpose CSV formats.
34
35The :mod:`csv` module's :class:`reader` and :class:`writer` objects read and
36write sequences. Programmers can also read and write data in dictionary form
37using the :class:`DictReader` and :class:`DictWriter` classes.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039.. seealso::
40
Georg Brandl116aa622007-08-15 14:28:22 +000041 :pep:`305` - CSV File API
42 The Python Enhancement Proposal which proposed this addition to Python.
43
44
45.. _csv-contents:
46
47Module Contents
48---------------
49
50The :mod:`csv` module defines the following functions:
51
52
R David Murray1b00f252012-08-15 10:43:58 -040053.. index::
54 single: universal newlines; csv.reader function
55
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000056.. function:: reader(csvfile, dialect='excel', **fmtparams)
Georg Brandl116aa622007-08-15 14:28:22 +000057
58 Return a reader object which will iterate over lines in the given *csvfile*.
Georg Brandl9afde1c2007-11-01 20:32:30 +000059 *csvfile* can be any object which supports the :term:`iterator` protocol and returns a
Georg Brandlb30f3302011-01-06 09:23:56 +000060 string each time its :meth:`!__next__` method is called --- :term:`file objects
Antoine Pitrou11cb9612010-09-15 11:11:28 +000061 <file object>` and list objects are both suitable. If *csvfile* is a file object,
R David Murray91887022011-03-19 22:30:14 -040062 it should be opened with ``newline=''``. [1]_ An optional
Georg Brandl116aa622007-08-15 14:28:22 +000063 *dialect* parameter can be given which is used to define a set of parameters
64 specific to a particular CSV dialect. It may be an instance of a subclass of
65 the :class:`Dialect` class or one of the strings returned by the
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000066 :func:`list_dialects` function. The other optional *fmtparams* keyword arguments
Georg Brandl116aa622007-08-15 14:28:22 +000067 can be given to override individual formatting parameters in the current
68 dialect. For full details about the dialect and formatting parameters, see
69 section :ref:`csv-fmt-params`.
70
Skip Montanaro0468df32009-03-25 00:52:18 +000071 Each row read from the csv file is returned as a list of strings. No
R. David Murray8b7d4aa2009-04-04 01:38:38 +000072 automatic data type conversion is performed unless the ``QUOTE_NONNUMERIC`` format
73 option is specified (in which case unquoted fields are transformed into floats).
Georg Brandl116aa622007-08-15 14:28:22 +000074
Christian Heimesb9eccbf2007-12-05 20:18:38 +000075 A short usage example::
Georg Brandl48310cd2009-01-03 21:18:54 +000076
Christian Heimesb9eccbf2007-12-05 20:18:38 +000077 >>> import csv
Ezio Melottie34f8a92012-09-15 05:51:45 +030078 >>> with open('eggs.csv', newline='') as csvfile:
79 ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
80 ... for row in spamreader:
81 ... print(', '.join(row))
Christian Heimesb9eccbf2007-12-05 20:18:38 +000082 Spam, Spam, Spam, Spam, Spam, Baked Beans
83 Spam, Lovely Spam, Wonderful Spam
84
Georg Brandl116aa622007-08-15 14:28:22 +000085
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000086.. function:: writer(csvfile, dialect='excel', **fmtparams)
Georg Brandl116aa622007-08-15 14:28:22 +000087
88 Return a writer object responsible for converting the user's data into delimited
89 strings on the given file-like object. *csvfile* can be any object with a
R David Murray9c0d5ea2011-03-20 11:18:21 -040090 :func:`write` method. If *csvfile* is a file object, it should be opened with
91 ``newline=''`` [1]_. An optional *dialect*
Georg Brandl116aa622007-08-15 14:28:22 +000092 parameter can be given which is used to define a set of parameters specific to a
93 particular CSV dialect. It may be an instance of a subclass of the
94 :class:`Dialect` class or one of the strings returned by the
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000095 :func:`list_dialects` function. The other optional *fmtparams* keyword arguments
Georg Brandl116aa622007-08-15 14:28:22 +000096 can be given to override individual formatting parameters in the current
97 dialect. For full details about the dialect and formatting parameters, see
98 section :ref:`csv-fmt-params`. To make it
99 as easy as possible to interface with modules which implement the DB API, the
100 value :const:`None` is written as the empty string. While this isn't a
101 reversible transformation, it makes it easier to dump SQL NULL data values to
102 CSV files without preprocessing the data returned from a ``cursor.fetch*`` call.
103 All other non-string data are stringified with :func:`str` before being written.
104
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000105 A short usage example::
106
Ezio Melottie34f8a92012-09-15 05:51:45 +0300107 import csv
108 with open('eggs.csv', 'w', newline='') as csvfile:
109 spamwriter = csv.writer(csvfile, delimiter=' ',
110 quotechar='|', quoting=csv.QUOTE_MINIMAL)
111 spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
112 spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000113
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Berker Peksag12b50ce2015-06-05 15:17:51 +0300115.. function:: register_dialect(name[, dialect[, **fmtparams]])
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Georg Brandlf6945182008-02-01 11:56:49 +0000117 Associate *dialect* with *name*. *name* must be a string. The
Georg Brandl116aa622007-08-15 14:28:22 +0000118 dialect can be specified either by passing a sub-class of :class:`Dialect`, or
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000119 by *fmtparams* keyword arguments, or both, with keyword arguments overriding
Georg Brandl116aa622007-08-15 14:28:22 +0000120 parameters of the dialect. For full details about the dialect and formatting
121 parameters, see section :ref:`csv-fmt-params`.
122
123
124.. function:: unregister_dialect(name)
125
126 Delete the dialect associated with *name* from the dialect registry. An
127 :exc:`Error` is raised if *name* is not a registered dialect name.
128
129
130.. function:: get_dialect(name)
131
Georg Brandl6554cb92007-12-02 23:15:43 +0000132 Return the dialect associated with *name*. An :exc:`Error` is raised if
133 *name* is not a registered dialect name. This function returns an immutable
134 :class:`Dialect`.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. function:: list_dialects()
137
138 Return the names of all registered dialects.
139
140
141.. function:: field_size_limit([new_limit])
142
143 Returns the current maximum field size allowed by the parser. If *new_limit* is
144 given, this becomes the new limit.
145
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147The :mod:`csv` module defines the following classes:
148
Berker Peksag631ada82017-01-07 09:32:56 +0300149.. class:: DictReader(f, fieldnames=None, restkey=None, restval=None, \
R David Murrayf031a6f2014-02-24 15:32:54 -0500150 dialect='excel', *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Raymond Hettinger43ca4522016-08-30 12:35:50 -0700152 Create an object that operates like a regular reader but maps the
Michael Selik9f3f0932019-01-31 00:47:53 -0800153 information in each row to a :class:`dict` whose keys are given by the
154 optional *fieldnames* parameter.
Raymond Hettinger43ca4522016-08-30 12:35:50 -0700155
156 The *fieldnames* parameter is a :term:`sequence`. If *fieldnames* is
Berker Peksag631ada82017-01-07 09:32:56 +0300157 omitted, the values in the first row of file *f* will be used as the
Michael Selik9f3f0932019-01-31 00:47:53 -0800158 fieldnames. Regardless of how the fieldnames are determined, the
Raymond Hettinger43ca4522016-08-30 12:35:50 -0700159 dictionary preserves their original ordering.
160
161 If a row has more fields than fieldnames, the remaining data is put in a
162 list and stored with the fieldname specified by *restkey* (which defaults
163 to ``None``). If a non-blank row has fewer fields than fieldnames, the
Juhana Jauhiainen4b3252c2020-03-24 19:11:42 +0200164 missing values are filled-in with the value of *restval* (which defaults
165 to ``None``).
Raymond Hettinger43ca4522016-08-30 12:35:50 -0700166
167 All other optional or keyword arguments are passed to the underlying
168 :class:`reader` instance.
169
Michael Selik9f3f0932019-01-31 00:47:53 -0800170 .. versionchanged:: 3.8
171 Returned rows are now of type :class:`dict`.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Berker Peksagc8c64e32014-11-24 23:46:30 +0200173 A short usage example::
174
175 >>> import csv
Ammar Askar275d2d92017-10-26 04:27:46 -0400176 >>> with open('names.csv', newline='') as csvfile:
Berker Peksagc8c64e32014-11-24 23:46:30 +0200177 ... reader = csv.DictReader(csvfile)
178 ... for row in reader:
179 ... print(row['first_name'], row['last_name'])
180 ...
Raymond Hettinger43ca4522016-08-30 12:35:50 -0700181 Eric Idle
182 John Cleese
183
184 >>> print(row)
Michael Selik9f3f0932019-01-31 00:47:53 -0800185 {'first_name': 'John', 'last_name': 'Cleese'}
Berker Peksagc8c64e32014-11-24 23:46:30 +0200186
Georg Brandl116aa622007-08-15 14:28:22 +0000187
Berker Peksag631ada82017-01-07 09:32:56 +0300188.. class:: DictWriter(f, fieldnames, restval='', extrasaction='raise', \
R David Murrayf031a6f2014-02-24 15:32:54 -0500189 dialect='excel', *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000190
R David Murrayf031a6f2014-02-24 15:32:54 -0500191 Create an object which operates like a regular writer but maps dictionaries
192 onto output rows. The *fieldnames* parameter is a :mod:`sequence
193 <collections.abc>` of keys that identify the order in which values in the
Berker Peksag631ada82017-01-07 09:32:56 +0300194 dictionary passed to the :meth:`writerow` method are written to file
195 *f*. The optional *restval* parameter specifies the value to be
R David Murrayf031a6f2014-02-24 15:32:54 -0500196 written if the dictionary is missing a key in *fieldnames*. If the
197 dictionary passed to the :meth:`writerow` method contains a key not found in
198 *fieldnames*, the optional *extrasaction* parameter indicates what action to
INADA Naoki0a421a22016-10-21 19:47:57 +0900199 take.
200 If it is set to ``'raise'``, the default value, a :exc:`ValueError`
201 is raised.
202 If it is set to ``'ignore'``, extra values in the dictionary are ignored.
203 Any other optional or keyword arguments are passed to the underlying
204 :class:`writer` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000205
R David Murrayf031a6f2014-02-24 15:32:54 -0500206 Note that unlike the :class:`DictReader` class, the *fieldnames* parameter
Andrés Delfino68606292018-06-08 09:51:12 -0300207 of the :class:`DictWriter` class is not optional.
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Berker Peksagc8c64e32014-11-24 23:46:30 +0200209 A short usage example::
210
211 import csv
212
Ammar Askar275d2d92017-10-26 04:27:46 -0400213 with open('names.csv', 'w', newline='') as csvfile:
Berker Peksagc8c64e32014-11-24 23:46:30 +0200214 fieldnames = ['first_name', 'last_name']
215 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
216
217 writer.writeheader()
218 writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
219 writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
220 writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223.. class:: Dialect
224
225 The :class:`Dialect` class is a container class relied on primarily for its
226 attributes, which are used to define the parameters for a specific
227 :class:`reader` or :class:`writer` instance.
228
229
230.. class:: excel()
231
232 The :class:`excel` class defines the usual properties of an Excel-generated CSV
233 file. It is registered with the dialect name ``'excel'``.
234
235
236.. class:: excel_tab()
237
238 The :class:`excel_tab` class defines the usual properties of an Excel-generated
239 TAB-delimited file. It is registered with the dialect name ``'excel-tab'``.
240
241
Georg Brandl7424dd32010-10-27 07:27:06 +0000242.. class:: unix_dialect()
243
244 The :class:`unix_dialect` class defines the usual properties of a CSV file
245 generated on UNIX systems, i.e. using ``'\n'`` as line terminator and quoting
246 all fields. It is registered with the dialect name ``'unix'``.
247
248 .. versionadded:: 3.2
249
250
Georg Brandl116aa622007-08-15 14:28:22 +0000251.. class:: Sniffer()
252
253 The :class:`Sniffer` class is used to deduce the format of a CSV file.
254
Benjamin Petersone41251e2008-04-25 01:59:09 +0000255 The :class:`Sniffer` class provides two methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000257 .. method:: sniff(sample, delimiters=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Benjamin Petersone41251e2008-04-25 01:59:09 +0000259 Analyze the given *sample* and return a :class:`Dialect` subclass
260 reflecting the parameters found. If the optional *delimiters* parameter
261 is given, it is interpreted as a string containing possible valid
262 delimiter characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Benjamin Petersone41251e2008-04-25 01:59:09 +0000265 .. method:: has_header(sample)
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Benjamin Petersone41251e2008-04-25 01:59:09 +0000267 Analyze the sample text (presumed to be in CSV format) and return
268 :const:`True` if the first row appears to be a series of column headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Christian Heimes7f044312008-01-06 17:05:40 +0000270An example for :class:`Sniffer` use::
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Ammar Askar275d2d92017-10-26 04:27:46 -0400272 with open('example.csv', newline='') as csvfile:
Ezio Melottie34f8a92012-09-15 05:51:45 +0300273 dialect = csv.Sniffer().sniff(csvfile.read(1024))
274 csvfile.seek(0)
275 reader = csv.reader(csvfile, dialect)
276 # ... process CSV file contents here ...
Christian Heimes7f044312008-01-06 17:05:40 +0000277
278
279The :mod:`csv` module defines the following constants:
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281.. data:: QUOTE_ALL
282
283 Instructs :class:`writer` objects to quote all fields.
284
285
286.. data:: QUOTE_MINIMAL
287
288 Instructs :class:`writer` objects to only quote those fields which contain
289 special characters such as *delimiter*, *quotechar* or any of the characters in
290 *lineterminator*.
291
292
293.. data:: QUOTE_NONNUMERIC
294
295 Instructs :class:`writer` objects to quote all non-numeric fields.
296
297 Instructs the reader to convert all non-quoted fields to type *float*.
298
299
300.. data:: QUOTE_NONE
301
302 Instructs :class:`writer` objects to never quote fields. When the current
303 *delimiter* occurs in output data it is preceded by the current *escapechar*
304 character. If *escapechar* is not set, the writer will raise :exc:`Error` if
305 any characters that require escaping are encountered.
306
307 Instructs :class:`reader` to perform no special processing of quote characters.
308
309The :mod:`csv` module defines the following exception:
310
311
312.. exception:: Error
313
314 Raised by any of the functions when an error is detected.
315
Georg Brandl116aa622007-08-15 14:28:22 +0000316.. _csv-fmt-params:
317
318Dialects and Formatting Parameters
319----------------------------------
320
321To make it easier to specify the format of input and output records, specific
322formatting parameters are grouped together into dialects. A dialect is a
323subclass of the :class:`Dialect` class having a set of specific methods and a
324single :meth:`validate` method. When creating :class:`reader` or
325:class:`writer` objects, the programmer can specify a string or a subclass of
326the :class:`Dialect` class as the dialect parameter. In addition to, or instead
327of, the *dialect* parameter, the programmer can also specify individual
328formatting parameters, which have the same names as the attributes defined below
329for the :class:`Dialect` class.
330
331Dialects support the following attributes:
332
333
334.. attribute:: Dialect.delimiter
335
336 A one-character string used to separate fields. It defaults to ``','``.
337
338
339.. attribute:: Dialect.doublequote
340
Zachary Ware79e50882015-09-11 10:51:47 -0500341 Controls how instances of *quotechar* appearing inside a field should
Georg Brandl116aa622007-08-15 14:28:22 +0000342 themselves be quoted. When :const:`True`, the character is doubled. When
343 :const:`False`, the *escapechar* is used as a prefix to the *quotechar*. It
344 defaults to :const:`True`.
345
346 On output, if *doublequote* is :const:`False` and no *escapechar* is set,
347 :exc:`Error` is raised if a *quotechar* is found in a field.
348
349
350.. attribute:: Dialect.escapechar
351
352 A one-character string used by the writer to escape the *delimiter* if *quoting*
353 is set to :const:`QUOTE_NONE` and the *quotechar* if *doublequote* is
354 :const:`False`. On reading, the *escapechar* removes any special meaning from
355 the following character. It defaults to :const:`None`, which disables escaping.
356
357
358.. attribute:: Dialect.lineterminator
359
360 The string used to terminate lines produced by the :class:`writer`. It defaults
361 to ``'\r\n'``.
362
363 .. note::
364
365 The :class:`reader` is hard-coded to recognise either ``'\r'`` or ``'\n'`` as
366 end-of-line, and ignores *lineterminator*. This behavior may change in the
367 future.
368
369
370.. attribute:: Dialect.quotechar
371
372 A one-character string used to quote fields containing special characters, such
373 as the *delimiter* or *quotechar*, or which contain new-line characters. It
374 defaults to ``'"'``.
375
376
377.. attribute:: Dialect.quoting
378
379 Controls when quotes should be generated by the writer and recognised by the
380 reader. It can take on any of the :const:`QUOTE_\*` constants (see section
381 :ref:`csv-contents`) and defaults to :const:`QUOTE_MINIMAL`.
382
383
384.. attribute:: Dialect.skipinitialspace
385
386 When :const:`True`, whitespace immediately following the *delimiter* is ignored.
387 The default is :const:`False`.
388
389
Ezio Melottia69be282012-11-18 12:55:35 +0200390.. attribute:: Dialect.strict
391
392 When ``True``, raise exception :exc:`Error` on bad CSV input.
393 The default is ``False``.
394
Georg Brandl116aa622007-08-15 14:28:22 +0000395Reader Objects
396--------------
397
398Reader objects (:class:`DictReader` instances and objects returned by the
399:func:`reader` function) have the following public methods:
400
Georg Brandlc7485062009-04-01 15:53:15 +0000401.. method:: csvreader.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Amit Kumard618c8c2017-05-23 10:39:42 +0530403 Return the next row of the reader's iterable object as a list (if the object
404 was returned from :func:`reader`) or a dict (if it is a :class:`DictReader`
405 instance), parsed according to the current dialect. Usually you should call
406 this as ``next(reader)``.
Georg Brandlc7485062009-04-01 15:53:15 +0000407
Georg Brandl116aa622007-08-15 14:28:22 +0000408
409Reader objects have the following public attributes:
410
Georg Brandl116aa622007-08-15 14:28:22 +0000411.. attribute:: csvreader.dialect
412
413 A read-only description of the dialect in use by the parser.
414
415
416.. attribute:: csvreader.line_num
417
418 The number of lines read from the source iterator. This is not the same as the
419 number of records returned, as records can span multiple lines.
420
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Skip Montanaroaf8fcfa2008-08-09 19:44:22 +0000422DictReader objects have the following public attribute:
423
Skip Montanaroaf8fcfa2008-08-09 19:44:22 +0000424.. attribute:: csvreader.fieldnames
425
426 If not passed as a parameter when creating the object, this attribute is
427 initialized upon first access or when the first record is read from the
428 file.
429
Skip Montanaroaf8fcfa2008-08-09 19:44:22 +0000430
431
Georg Brandl116aa622007-08-15 14:28:22 +0000432Writer Objects
433--------------
434
435:class:`Writer` objects (:class:`DictWriter` instances and objects returned by
436the :func:`writer` function) have the following public methods. A *row* must be
Serhiy Storchaka7901b482015-03-30 09:09:54 +0300437an iterable of strings or numbers for :class:`Writer` objects and a dictionary
Georg Brandl116aa622007-08-15 14:28:22 +0000438mapping fieldnames to strings or numbers (by passing them through :func:`str`
439first) for :class:`DictWriter` objects. Note that complex numbers are written
440out surrounded by parens. This may cause some problems for other programs which
441read CSV files (assuming they support complex numbers at all).
442
443
444.. method:: csvwriter.writerow(row)
445
446 Write the *row* parameter to the writer's file object, formatted according to
Rémi Lapeyrefce5ff12019-05-10 03:50:11 +0200447 the current dialect. Return the return value of the call to the *write* method
448 of the underlying file object.
Georg Brandl116aa622007-08-15 14:28:22 +0000449
Serhiy Storchaka7901b482015-03-30 09:09:54 +0300450 .. versionchanged:: 3.5
451 Added support of arbitrary iterables.
Georg Brandl116aa622007-08-15 14:28:22 +0000452
453.. method:: csvwriter.writerows(rows)
454
Zackery Spytza801cf12018-06-02 09:02:16 -0600455 Write all elements in *rows* (an iterable of *row* objects as described
456 above) to the writer's file object, formatted according to the current
457 dialect.
Georg Brandl116aa622007-08-15 14:28:22 +0000458
459Writer objects have the following public attribute:
460
461
462.. attribute:: csvwriter.dialect
463
464 A read-only description of the dialect in use by the writer.
465
466
R. David Murraybe0698b2010-02-23 22:57:58 +0000467DictWriter objects have the following public method:
468
469
470.. method:: DictWriter.writeheader()
471
Rémi Lapeyrefce5ff12019-05-10 03:50:11 +0200472 Write a row with the field names (as specified in the constructor) to
473 the writer's file object, formatted according to the current dialect. Return
474 the return value of the :meth:`csvwriter.writerow` call used internally.
R. David Murraybe0698b2010-02-23 22:57:58 +0000475
R. David Murray19e45482010-02-23 23:00:34 +0000476 .. versionadded:: 3.2
Rémi Lapeyrefce5ff12019-05-10 03:50:11 +0200477 .. versionchanged:: 3.8
478 :meth:`writeheader` now also returns the value returned by
479 the :meth:`csvwriter.writerow` method it uses internally.
R. David Murraybe0698b2010-02-23 22:57:58 +0000480
481
Georg Brandl116aa622007-08-15 14:28:22 +0000482.. _csv-examples:
483
484Examples
485--------
486
487The simplest example of reading a CSV file::
488
489 import csv
Eli Bendersky6860a922011-03-11 15:47:36 +0200490 with open('some.csv', newline='') as f:
491 reader = csv.reader(f)
492 for row in reader:
493 print(row)
Georg Brandl116aa622007-08-15 14:28:22 +0000494
495Reading a file with an alternate format::
496
497 import csv
R David Murray91887022011-03-19 22:30:14 -0400498 with open('passwd', newline='') as f:
Eli Bendersky6860a922011-03-11 15:47:36 +0200499 reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
500 for row in reader:
501 print(row)
Georg Brandl116aa622007-08-15 14:28:22 +0000502
503The corresponding simplest possible writing example is::
504
505 import csv
R David Murray91887022011-03-19 22:30:14 -0400506 with open('some.csv', 'w', newline='') as f:
Eli Bendersky6860a922011-03-11 15:47:36 +0200507 writer = csv.writer(f)
508 writer.writerows(someiterable)
Georg Brandl116aa622007-08-15 14:28:22 +0000509
R. David Murray8b7d4aa2009-04-04 01:38:38 +0000510Since :func:`open` is used to open a CSV file for reading, the file
511will by default be decoded into unicode using the system default
512encoding (see :func:`locale.getpreferredencoding`). To decode a file
513using a different encoding, use the ``encoding`` argument of open::
514
Eli Bendersky6860a922011-03-11 15:47:36 +0200515 import csv
516 with open('some.csv', newline='', encoding='utf-8') as f:
517 reader = csv.reader(f)
518 for row in reader:
519 print(row)
R. David Murray8b7d4aa2009-04-04 01:38:38 +0000520
521The same applies to writing in something other than the system default
522encoding: specify the encoding argument when opening the output file.
523
Georg Brandl116aa622007-08-15 14:28:22 +0000524Registering a new dialect::
525
526 import csv
Georg Brandl116aa622007-08-15 14:28:22 +0000527 csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
R David Murray91887022011-03-19 22:30:14 -0400528 with open('passwd', newline='') as f:
Eli Bendersky6860a922011-03-11 15:47:36 +0200529 reader = csv.reader(f, 'unixpwd')
Georg Brandl116aa622007-08-15 14:28:22 +0000530
531A slightly more advanced use of the reader --- catching and reporting errors::
532
533 import csv, sys
Eli Bendersky6860a922011-03-11 15:47:36 +0200534 filename = 'some.csv'
535 with open(filename, newline='') as f:
536 reader = csv.reader(f)
537 try:
538 for row in reader:
539 print(row)
540 except csv.Error as e:
541 sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543And while the module doesn't directly support parsing strings, it can easily be
544done::
545
546 import csv
547 for row in csv.reader(['one,two,three']):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000548 print(row)
Georg Brandl116aa622007-08-15 14:28:22 +0000549
Georg Brandl116aa622007-08-15 14:28:22 +0000550
R. David Murray8b7d4aa2009-04-04 01:38:38 +0000551.. rubric:: Footnotes
Georg Brandl116aa622007-08-15 14:28:22 +0000552
R David Murray91887022011-03-19 22:30:14 -0400553.. [1] If ``newline=''`` is not specified, newlines embedded inside quoted fields
554 will not be interpreted correctly, and on platforms that use ``\r\n`` linendings
R David Murray9c0d5ea2011-03-20 11:18:21 -0400555 on write an extra ``\r`` will be added. It should always be safe to specify
R David Murray1b00f252012-08-15 10:43:58 -0400556 ``newline=''``, since the csv module does its own
557 (:term:`universal <universal newlines>`) newline handling.