From 3fa714cbff72e99a5389530a8a9c79fa0c093316 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 16 Jun 2026 19:35:17 -0300 Subject: [PATCH 1/4] gh-151556: Show sample CSV file content in csv module docs Fixes gh-151556 --- Doc/library/csv.rst | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 21ecdbcc08f3486..24407c5ffbf20d3 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -71,6 +71,13 @@ The :mod:`!csv` module defines the following functions: automatic data type conversion is performed unless the :data:`QUOTE_NONNUMERIC` format option is specified (in which case unquoted fields are transformed into floats). + Assume that :file:`eggs.csv` contains: + + .. code-block:: text + + Spam Spam Spam Spam Spam |Baked Beans| + Spam |Lovely Spam| |Wonderful Spam| + A short usage example:: >>> import csv @@ -101,6 +108,13 @@ The :mod:`!csv` module defines the following functions: CSV files without preprocessing the data returned from a ``cursor.fetch*`` call. All other non-string data are stringified with :func:`str` before being written. + The example below writes :file:`eggs.csv` with this content: + + .. code-block:: text + + Spam Spam Spam Spam Spam |Baked Beans| + Spam |Lovely Spam| |Wonderful Spam| + A short usage example:: import csv @@ -177,6 +191,14 @@ The :mod:`!csv` module defines the following classes: .. versionchanged:: 3.8 Returned rows are now of type :class:`dict`. + Assume that :file:`names.csv` contains: + + .. code-block:: text + + first_name,last_name + Eric,Idle + John,Cleese + A short usage example:: >>> import csv @@ -215,6 +237,15 @@ The :mod:`!csv` module defines the following classes: If the argument passed to *fieldnames* is an iterator, it will be coerced to a :class:`list`. + The example below writes :file:`names.csv` with this content: + + .. code-block:: text + + first_name,last_name + Baked,Beans + Lovely,Spam + Wonderful,Spam + A short usage example:: import csv From ef44100bfa13c0f843703a006dfcbfb93a5419d7 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 17 Jun 2026 08:23:11 -0300 Subject: [PATCH 2/4] gh-151556: Place CSV sample output after writer examples --- Doc/library/csv.rst | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 24407c5ffbf20d3..5590a5c7af33a6b 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -108,13 +108,6 @@ The :mod:`!csv` module defines the following functions: CSV files without preprocessing the data returned from a ``cursor.fetch*`` call. All other non-string data are stringified with :func:`str` before being written. - The example below writes :file:`eggs.csv` with this content: - - .. code-block:: text - - Spam Spam Spam Spam Spam |Baked Beans| - Spam |Lovely Spam| |Wonderful Spam| - A short usage example:: import csv @@ -124,6 +117,13 @@ The :mod:`!csv` module defines the following functions: spamwriter.writerow(['Spam'] * 5 + ['Baked Beans']) spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) + The example writes :file:`eggs.csv` with this content: + + .. code-block:: text + + Spam Spam Spam Spam Spam |Baked Beans| + Spam |Lovely Spam| |Wonderful Spam| + .. function:: register_dialect(name, /, dialect='excel', **fmtparams) @@ -237,15 +237,6 @@ The :mod:`!csv` module defines the following classes: If the argument passed to *fieldnames* is an iterator, it will be coerced to a :class:`list`. - The example below writes :file:`names.csv` with this content: - - .. code-block:: text - - first_name,last_name - Baked,Beans - Lovely,Spam - Wonderful,Spam - A short usage example:: import csv @@ -259,6 +250,15 @@ The :mod:`!csv` module defines the following classes: writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'}) writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'}) + The example writes :file:`names.csv` with this content: + + .. code-block:: text + + first_name,last_name + Baked,Beans + Lovely,Spam + Wonderful,Spam + .. class:: Dialect From 41b98e4e756d070bd642724ac08e45ab69d4c3a4 Mon Sep 17 00:00:00 2001 From: lucas eduardo Date: Mon, 22 Jun 2026 13:53:35 -0300 Subject: [PATCH 3/4] gh-151556: Place CSV sample input after reader usage examples --- Doc/library/csv.rst | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 5590a5c7af33a6b..8a8f02af234dd2d 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -71,13 +71,6 @@ The :mod:`!csv` module defines the following functions: automatic data type conversion is performed unless the :data:`QUOTE_NONNUMERIC` format option is specified (in which case unquoted fields are transformed into floats). - Assume that :file:`eggs.csv` contains: - - .. code-block:: text - - Spam Spam Spam Spam Spam |Baked Beans| - Spam |Lovely Spam| |Wonderful Spam| - A short usage example:: >>> import csv @@ -88,6 +81,13 @@ The :mod:`!csv` module defines the following functions: Spam, Spam, Spam, Spam, Spam, Baked Beans Spam, Lovely Spam, Wonderful Spam + where :file:`eggs.csv` contains: + + .. code-block:: text + + Spam Spam Spam Spam Spam |Baked Beans| + Spam |Lovely Spam| |Wonderful Spam| + .. function:: writer(csvfile, /, dialect='excel', **fmtparams) @@ -191,14 +191,6 @@ The :mod:`!csv` module defines the following classes: .. versionchanged:: 3.8 Returned rows are now of type :class:`dict`. - Assume that :file:`names.csv` contains: - - .. code-block:: text - - first_name,last_name - Eric,Idle - John,Cleese - A short usage example:: >>> import csv @@ -213,6 +205,14 @@ The :mod:`!csv` module defines the following classes: >>> print(row) {'first_name': 'John', 'last_name': 'Cleese'} + where :file:`names.csv` contains: + + .. code-block:: text + + first_name,last_name + Eric,Idle + John,Cleese + .. class:: DictWriter(f, fieldnames, restval='', extrasaction='raise', \ dialect='excel', *args, **kwds) From 3da4caa399c55084a45c693d20bb6ff20e5f2acf Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 23 Jun 2026 08:51:29 -0300 Subject: [PATCH 4/4] gh-151556: Tweak wording of writer example file output Apply changes from code review. Co-authored-by: Stan Ulbrych Co-authored-by: Stan Ulbrych --- Doc/library/csv.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 8a8f02af234dd2d..87c2d4702e74086 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -117,7 +117,7 @@ The :mod:`!csv` module defines the following functions: spamwriter.writerow(['Spam'] * 5 + ['Baked Beans']) spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) - The example writes :file:`eggs.csv` with this content: + which writes :file:`eggs.csv` containing: .. code-block:: text @@ -250,7 +250,7 @@ The :mod:`!csv` module defines the following classes: writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'}) writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'}) - The example writes :file:`names.csv` with this content: + which writes :file:`names.csv` containing: .. code-block:: text