Skip to content
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
80be530
do not iterate over all ascii
maurycy Aug 11, 2025
2d636cf
NEWS entry
maurycy Aug 11, 2025
1f0b25e
bring back the comment
maurycy Aug 11, 2025
601b2f1
bang
maurycy Aug 11, 2025
2dc0d41
document the public method
maurycy Aug 11, 2025
f106da2
import within Sniffer
maurycy Aug 11, 2025
7f7dca1
Merge branch 'main' into csv-sniffer-counter-set
maurycy Aug 11, 2025
2f1ea73
_ASCII_CHARS, set operators
maurycy Aug 11, 2025
4b50610
Merge branch 'main' into csv-sniffer-counter-set
maurycy Aug 13, 2025
07a336b
update docs, no set operations
maurycy Aug 13, 2025
080dbfc
Merge branch 'main' into csv-sniffer-counter-set
maurycy Aug 15, 2025
0b5bcdd
Merge branch 'main' into csv-sniffer-counter-set
maurycy Aug 17, 2025
2ccaac0
Update Lib/csv.py
maurycy Aug 18, 2025
36fc9d9
move whatsnew to Optimizations
maurycy Aug 18, 2025
7189b51
s/seen/num_lines/
maurycy Aug 18, 2025
6b64ba4
picnixz suggestion
maurycy Aug 18, 2025
4b62c84
use isascii
maurycy Aug 18, 2025
dcbd9f5
CRLF tie
maurycy Aug 19, 2025
14ea139
simple tie test
maurycy Aug 19, 2025
c332fb4
rm assert
maurycy Aug 19, 2025
e5b5611
update the benchmark numbers
maurycy Aug 19, 2025
8614756
Merge branch 'main' into csv-sniffer-counter-set
maurycy Aug 19, 2025
a95b16a
Update Lib/test/test_csv.py
maurycy Aug 28, 2025
59c9395
vs
maurycy Aug 28, 2025
3e7beaa
Update Doc/whatsnew/3.15.rst
maurycy Aug 28, 2025
c9cd73e
Update Lib/csv.py
maurycy Aug 28, 2025
7a3c974
,: tie
maurycy Aug 30, 2025
cecb897
be consistent
maurycy Aug 30, 2025
ab871e0
check the exc msg
maurycy Aug 30, 2025
5181f5d
Update Lib/test/test_csv.py
maurycy Aug 30, 2025
018e580
s/charFrequency/char_frequency/
maurycy Sep 11, 2025
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
9 changes: 5 additions & 4 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,12 @@ zlib
Optimizations
=============

module_name
-----------

* TODO
csv
---

* The :meth:`csv.Sniffer.sniff` delimiter detection has been optimized,
and is now up to 1.6x faster.
(Contributed by Maurycy Pawłowski-Wieroński in :gh:`137628`.)


Deprecated
Expand Down
30 changes: 17 additions & 13 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,31 +364,35 @@ def _guess_delimiter(self, data, delimiters):
try and evaluate the smallest portion of the data possible, evaluating
additional chunks as necessary.
"""
from collections import Counter, defaultdict

data = list(filter(None, data.split('\n')))

ascii = [chr(c) for c in range(127)] # 7-bit ASCII

# build frequency tables
chunkLength = min(10, len(data))
iteration = 0
charFrequency = {}
num_lines = 0
# {char -> {count_per_line -> num_lines_with_that_count}}
charFrequency = defaultdict(Counter)
modes = {}
delims = {}
start, end = 0, chunkLength
while start < len(data):
iteration += 1
for line in data[start:end]:
for char in ascii:
metaFrequency = charFrequency.get(char, {})
# must count even if frequency is 0
freq = line.count(char)
# value is the mode
metaFrequency[freq] = metaFrequency.get(freq, 0) + 1
charFrequency[char] = metaFrequency

for char in charFrequency.keys():
items = list(charFrequency[char].items())
num_lines += 1
for char, count in Counter(line).items():
if char.isascii():
charFrequency[char][count] += 1

for char, counts in charFrequency.items():
items = list(counts.items())
missed_lines = num_lines - sum(counts.values())
if missed_lines:
# charFrequency[char][0] can only be deduced now
# as it cannot be obtained when parsing the lines.
# Store the number of lines 'char' was missing from.
items.append((0, missed_lines))
if len(items) == 1 and items[0][0] == 0:
continue
# get the mode of the frequencies
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,31 @@ def test_doublequote(self):
dialect = sniffer.sniff(self.sample9)
self.assertTrue(dialect.doublequote)

def test_guess_delimiter_crlf_not_chosen(self):
# Ensure that we pick the real delimiter ("|") over "\r" in a tie.
sniffer = csv.Sniffer()
sample = "a|b\r\nc|d\r\ne|f\r\n"
self.assertEqual(sniffer.sniff(sample).delimiter, "|")
self.assertNotEqual(sniffer.sniff(sample).delimiter, "\r")

def test_zero_mode_tie_order_independence(self):
# ":" appears in half the rows (1, 0, 1, 0) - a tie between
# 0 and 1 per line.
# "," appears once every row (true delimiter).
#
# Even if the zero-frequency bucket is appended v. inserted, the tie
# yields an adjusted score of 0, so ":" should not be promoted and
# "," must be selected.
sample = (
"a,b:c\n"
"d,e\n"
"f,g:c\n"
"h,i\n"
)
sniffer = csv.Sniffer()
dialect = sniffer.sniff(sample)
self.assertEqual(dialect.delimiter, ",")

class NUL:
def write(s, *args):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up :meth:`csv.Sniffer.sniff` delimiter detection by up to 1.6x.
Loading