Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91bb70c

Browse files
author
Skip Montanaro
committed
Fix for problem with Sniffer class. If your delimiter is whitespace and the
last field was empty it would strip the delimiter and incorrectly guess that "" was the delimiter. Reported in c.l.py by Laurent Laporte. Will backport.
1 parent e08fa29 commit 91bb70c

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

Lib/csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def _guess_delimiter(self, data, delimiters):
271271
for char in ascii:
272272
metaFrequency = charFrequency.get(char, {})
273273
# must count even if frequency is 0
274-
freq = line.strip().count(char)
274+
freq = line.count(char)
275275
# value is the mode
276276
metaFrequency[freq] = metaFrequency.get(freq, 0) + 1
277277
charFrequency[char] = metaFrequency

Lib/test/test_csv.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,6 @@ class TestSniffer(unittest.TestCase):
836836
'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow'
837837
'Stonecutters Seafood and Chop House':'Lemont':'IL':'12/19/02':'Week Back'
838838
"""
839-
840839
header = '''\
841840
"venue","city","state","date","performers"
842841
'''
@@ -852,6 +851,8 @@ class TestSniffer(unittest.TestCase):
852851
47483648;43.0;170;abc;def
853852
'''
854853

854+
sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n"
855+
855856
def test_has_header(self):
856857
sniffer = csv.Sniffer()
857858
self.assertEqual(sniffer.has_header(self.sample1), False)
@@ -879,6 +880,8 @@ def test_delimiters(self):
879880
self.assertEqual(dialect.delimiter, "/")
880881
dialect = sniffer.sniff(self.sample4)
881882
self.assertEqual(dialect.delimiter, ";")
883+
dialect = sniffer.sniff(self.sample5)
884+
self.assertEqual(dialect.delimiter, "\t")
882885

883886
if not hasattr(sys, "gettotalrefcount"):
884887
if test_support.verbose: print "*** skipping leakage tests ***"

0 commit comments

Comments
 (0)