E57C Issue #18219: Optimize csv.DictWriter for large number of columns. · python/cpython@0a421a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a421a2

Browse files
committed
Issue #18219: Optimize csv.DictWriter for large number of columns.
Patch by Mariatta Wijaya.
1 parent 4510e6d commit 0a421a2

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

Doc/library/csv.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,12 @@ The :mod:`csv` module defines the following classes:
195195
written if the dictionary is missing a key in *fieldnames*. If the
196196
dictionary passed to the :meth:`writerow` method contains a key not found in
197197
*fieldnames*, the optional *extrasaction* parameter indicates what action to
198-
take. If it is set to ``'raise'`` a :exc:`ValueError` is raised. If it is
199-
set to ``'ignore'``, extra values in the dictionary are ignored. Any other
200-
optional or keyword arguments are passed to the underlying :class:`writer`
201-
instance.
198+
take.
199+
If it is set to ``'raise'``, the default value, a :exc:`ValueError`
200+
is raised.
201+
If it is set to ``'ignore'``, extra values in the dictionary are ignored.
202+
Any other optional or keyword arguments are passed to the underlying
203+
:class:`writer` instance.
202204

203205
Note that unlike the :class:`DictReader` class, the *fieldnames* parameter
204206
of the :class:`DictWriter` is not optional. Since Python's :class:`dict`

Lib/csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def writeheader(self):
145145

146146
def _dict_to_list(self, rowdict):
147147
if self.extrasaction == "raise":
148-
wrong_fields = [k for k in rowdict if k not in self.fieldnames]
148+
wrong_fields = rowdict.keys() - self.fieldnames
149149
if wrong_fields:
150150
raise ValueError("dict contains fields not in fieldnames: "
151151
+ ", ".join([repr(x) for x in wrong_fields]))

Lib/test/test_csv.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,24 @@ def test_write_fields_not_in_fieldnames(self):
626626
self.assertNotIn("'f2'", exception)
627627
self.assertIn("1", exception)
628628

629+
def test_typo_in_extrasaction_raises_error(self):
630+
fileobj = StringIO()
631+
self.assertRaises(ValueError, csv.DictWriter, fileobj, ['f1', 'f2'],
632+
extrasaction="raised")
633+
634+
def test_write_field_not_in_field_names_raise(self):
635+
fileobj = StringIO()
636+
writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="raise")
637+
dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
638+
self.assertRaises(ValueError, csv.DictWriter.writerow, writer, dictrow)
639+
640+
def test_write_field_not_in_field_names_ignore(self):
641+
fileobj = StringIO()
642+
writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="ignore")
643+
dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
644+
csv.DictWriter.writerow(writer, dictrow)
645+
self.assertEqual(fileobj.getvalue(), "1,2\r\n")
646+
629647
def test_read_dict_fields(self):
630648
with TemporaryFile("w+") as fileobj:
631649
fileobj.write("1,2,abc\r\n")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Core and Builtins
2020
Library
2121
-------
2222

23+
- Issue #18219: Optimize csv.DictWriter for large number of columns.
24+
Patch by Mariatta Wijaya.
25+
2326
- Issue #28448: Fix C implemented asyncio.Future didn't work on Windows.
2427

2528
- Issue #28480: Fix error building socket module when multithreading is

0 commit comments

Comments
 (0)
0