Skip to content
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
6 changes: 5 additions & 1 deletion Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ def _guess_delimiter(self, data, delimiters):
"""
from collections import Counter, defaultdict

data = list(filter(None, data.split('\n')))
lines = data.split('\n')
if len(lines) > 1 and not data.endswith(('\r', '\n')):
# The sample may have been cut off in the middle of the last row.
lines.pop()
data = list(filter(None, lines))

# build frequency tables
chunkLength = min(10, len(data))
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,22 @@ def test_zero_mode_tie_order_colon_first(self):
with self.assertRaisesRegex(csv.Error, "Could not determine delimiter"):
sniffer.sniff(sample)

def test_sniff_truncated_sample(self):
sniffer = csv.Sniffer()
# A single row without a line terminator is complete enough to sniff.
self.assertEqual(sniffer.sniff("a,b").delimiter, ",")

# The last row may have been cut off in the middle of the sample.
for lineterminator in ("\n", "\r\n"):
with self.subTest(lineterminator=lineterminator):
sample = lineterminator.join((
"a,b,c",
"d,e,f",
"g,h,i",
"j,k",
))
self.assertEqual(sniffer.sniff(sample).delimiter, ",")


class NUL:
def write(s, *args):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`csv.Sniffer.sniff` failing to detect the delimiter when a sample
is truncated in the middle of its final row.
Loading