Skip to content

bpo-27497: Add return value to csv.DictWriter.writeheader #12306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Doc/library/csv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ read CSV files (assuming they support complex numbers at all).
.. method:: csvwriter.writerow(row)

Write the *row* parameter to the writer's file object, formatted according to
the current dialect.
the current dialect. Return the return value of the call to the *write* method
of the underlying file object.

.. versionchanged:: 3.5
Added support of arbitrary iterables.
Expand All @@ -467,9 +468,14 @@ DictWriter objects have the following public method:

.. method:: DictWriter.writeheader()

Write a row with the field names (as specified in the constructor).
Write a row with the field names (as specified in the constructor) to
the writer's file object, formatted according to the current dialect. Return
the return value of the :meth:`csvwriter.writerow` call used internally.

.. versionadded:: 3.2
.. versionchanged:: 3.8
:meth:`writeheader` now also returns the value returned by
the :meth:`csvwriter.writerow` method it uses internally.


.. _csv-examples:
Expand Down
2 changes: 1 addition & 1 deletion Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, f, fieldnames, restval="", extrasaction="raise",

def writeheader(self):
header = dict(zip(self.fieldnames, self.fieldnames))
self.writerow(header)
return self.writerow(header)

def _dict_to_list(self, rowdict):
if self.extrasaction == "raise":
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ def test_read_escape_fieldsep(self):
class TestDictFields(unittest.TestCase):
### "long" means the row is longer than the number of fieldnames
### "short" means there are fewer elements in the row than fieldnames
def test_writeheader_return_value(self):
with TemporaryFile("w+", newline='') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
writeheader_return_value = writer.writeheader()
self.assertEqual(writeheader_return_value, 10)

def test_write_simple_dict(self):
with TemporaryFile("w+", newline='') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:meth:`csv.DictWriter.writeheader` now returns the return value of the
underlying :meth:`csv.Writer.writerow` method. Patch contributed by Ashish
Nitin Patil.