From 1b3d9930373f40495a60c48783b18f5e25b9afca Mon Sep 17 00:00:00 2001 From: subotac <73706465+subotac@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:39:13 +0200 Subject: [PATCH] gh-162737: Preserve leading spaces in minimally quoted CSV fields --- Doc/library/csv.rst | 6 ++++-- Lib/test/test_csv.py | 15 +++++++++++++++ ...2026-11-28-12-43-00.gh-issue-162737.z0WDkS.rst | 2 ++ Modules/_csv.c | 5 +++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-28-12-43-00.gh-issue-162737.z0WDkS.rst diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 52d7bf20228541..c8104c2c008197 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -388,9 +388,11 @@ The :mod:`!csv` module defines the following constants: .. data:: QUOTE_MINIMAL - Instructs :class:`writer` objects to only quote those fields which contain - special characters such as *delimiter*, *quotechar*, ``'\r'``, ``'\n'`` + Instructs :class:`writer` objects to quote fields containing special + characters such as *delimiter*, *quotechar*, ``'\r'``, ``'\n'`` or any of the characters in *lineterminator*. + It also quotes fields starting with a space when + :attr:`~Dialect.skipinitialspace` is :const:`True`. If *doublequote* is :const:`False` and *escapechar* is set, the *quotechar* is escaped instead of causing the field to be quoted. diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 056d7551cfb8e5..1811925f91cc21 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -232,6 +232,21 @@ def test_write_quoting(self): 'a,1,"p,q","r""s",x!y', quotechar='"') + def test_write_quoting_leading_spaces(self): + fields = [' a', 'b c', ' d', 'e '] + for skipinitialspace, expected in [ + (True, '" a",b c," d",e '), + (False, ' a,b c, d,e '), + ]: + with self.subTest(skipinitialspace=skipinitialspace): + with StringIO(newline='') as fileobj: + writer = csv.writer(fileobj, skipinitialspace=skipinitialspace) + writer.writerow(fields) + fileobj.seek(0) + reader = csv.reader(fileobj, writer.dialect) + self.assertEqual(next(reader), fields) + self.assertEqual(fileobj.getvalue(), expected + '\r\n') + def test_write_delimiter(self): self._write_test(['a', 1, 'p,q', 'x;y'], 'a,1,"p,q",x;y') self._write_test(['a', 1, 'p;q', 'x,y'], 'a;1;"p;q";x,y', delimiter=';') diff --git a/Misc/NEWS.d/next/Library/2026-11-28-12-43-00.gh-issue-162737.z0WDkS.rst b/Misc/NEWS.d/next/Library/2026-11-28-12-43-00.gh-issue-162737.z0WDkS.rst new file mode 100644 index 00000000000000..9da49964f5051d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-28-12-43-00.gh-issue-162737.z0WDkS.rst @@ -0,0 +1,2 @@ +Quote fields with leading spaces in :class:`csv.writer` when +:const:`csv.QUOTE_MINIMAL` and ``skipinitialspace=True`` are used. diff --git a/Modules/_csv.c b/Modules/_csv.c index 484f7bc0f9d2d4..a417e4a4c21472 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -1373,6 +1373,11 @@ join_append(WriterObj *self, PyObject *field, int quoted) field_kind = PyUnicode_KIND(field); field_data = PyUnicode_DATA(field); field_len = PyUnicode_GET_LENGTH(field); + if (dialect->quoting == QUOTE_MINIMAL && dialect->skipinitialspace && + field_len > 0 && PyUnicode_READ(field_kind, field_data, 0) == ' ') + { + quoted = 1; + } } if (!field_len && dialect->delimiter == ' ' && dialect->skipinitialspace) { if (dialect->quoting == QUOTE_NONE ||