]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-83273: Rewrite csv.Sniffer dialect detection using trial parsing (GH-153694)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 21 Jul 2026 06:22:53 +0000 (09:22 +0300)
committerGitHub <noreply@github.com>
Tue, 21 Jul 2026 06:22:53 +0000 (09:22 +0300)
commit25fea6d64e194c8998f5752e74b646354dbd7a57
treeca256a34f0765a219ac0ea93051d682c597a554f
parent63a2709b700d5532b8925caca5bc8b9437e7a1f3
gh-83273: Rewrite csv.Sniffer dialect detection using trial parsing (GH-153694)

Guess the dialect by parsing the sample with every plausible
combination of delimiter, quotechar and escapechar, using the actual
CSV parser in strict mode, and choosing the combination which splits
the sample into rows with the most consistent number of fields.  The
old heuristics, which guessed the delimiter from characters adjacent
to quotes and from character frequencies, are removed.

A large sample is parsed incrementally: first only its beginning,
then, after eliminating the combinations which are clearly worse than
the leader, a several times larger part, and so on.

* csv.Sniffer can now detect escapechar='\\'.
* Explicitly requested delimiters are no longer restricted to ASCII.
* A delimiter inside a quoted field no longer wins over the actual
  delimiter, and sniffing no longer takes quadratic time on quoted
  samples.
* The sample can be cut off at an arbitrary point: in the middle of a
  row, of a quoted field or of an escaped sequence.
* Only '\r', '\n' and '\r\n' are treated as row separators, so
  characters like '\x1c' can now be detected as a delimiter.
* A preamble (title or comment lines) before the data does not prevent
  detection if the data rows outnumber the preamble lines.
* A sample consisting of a single column of quoted fields now raises
  csv.Error instead of guessing a delimiter from the content of the
  fields, and Sniffer.has_header() no longer raises ValueError for
  such samples.
* doublequote is detected by comparing the two readings of the sample.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Doc/library/csv.rst
Doc/whatsnew/3.16.rst
Lib/csv.py
Lib/test/test_csv.py
Misc/NEWS.d/next/Library/2026-07-14-12-00-00.gh-issue-83273.SnifQ7.rst [new file with mode: 0644]