Skip to content

gh-137627: Make csv.Sniffer.sniff() delimiter detection 1.6x faster #137628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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