Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
29 changes: 18 additions & 11 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class excel:
__version__ = "1.0"


_ASCII_CHARS = frozenset(map(chr, range(127))) # 7-bit ASCII

class Dialect:
"""Describe a CSV dialect.

Expand Down Expand Up @@ -364,28 +366,33 @@ 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 = {}
# {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
chunk = data[start:end]
candidate_chars = set().union(*chunk)
candidate_chars &= _ASCII_CHARS
for line in chunk:
for char in candidate_chars:
count = line.count(char)
charFrequency[char][count] += 1

# must count even if frequency is 0
missing_chars = _ASCII_CHARS - candidate_chars
chunk_len = len(chunk)
for char in missing_chars:
charFrequency[char][0] += chunk_len

for char in charFrequency.keys():
items = list(charFrequency[char].items())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`csv.Sniffer.sniff` 2x faster
Loading