Skip to content

Commit fce5ff1

Browse files
Rémi Lapeyrevstinner
authored andcommitted
bpo-27497: Add return value to csv.DictWriter.writeheader (GH-12306)
csv.DictWriter.writeheader() now returns the return value of the underlying csv.Writer.writerow() method. Patch contributed by Ashish Nitin Patil.
1 parent d237b3f commit fce5ff1

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

Doc/library/csv.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ read CSV files (assuming they support complex numbers at all).
443443
.. method:: csvwriter.writerow(row)
444444

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

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

468469
.. method:: DictWriter.writeheader()
469470

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

472475
.. versionadded:: 3.2
476+
.. versionchanged:: 3.8
477+
:meth:`writeheader` now also returns the value returned by
478+
the :meth:`csvwriter.writerow` method it uses internally.
473479

474480

475481
.. _csv-examples:

Lib/csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def __init__(self, f, fieldnames, restval="", extrasaction="raise",
140140

141141
def writeheader(self):
142142
header = dict(zip(self.fieldnames, self.fieldnames))
143-
self.writerow(header)
143+
return self.writerow(header)
144144

145145
def _dict_to_list(self, rowdict):
146146
if self.extrasaction == "raise":

Lib/test/test_csv.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,12 @@ def test_read_escape_fieldsep(self):
608608
class TestDictFields(unittest.TestCase):
609609
### "long" means the row is longer than the number of fieldnames
610610
### "short" means there are fewer elements in the row than fieldnames
611+
def test_writeheader_return_value(self):
612+
with TemporaryFile("w+", newline='') as fileobj:
613+
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
614+
writeheader_return_value = writer.writeheader()
615+
self.assertEqual(writeheader_return_value, 10)
616+
611617
def test_write_simple_dict(self):
612618
with TemporaryFile("w+", newline='') as fileobj:
613619
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:meth:`csv.DictWriter.writeheader` now returns the return value of the
2+
underlying :meth:`csv.Writer.writerow` method. Patch contributed by Ashish
3+
Nitin Patil.

0 commit comments

Comments
 (0)