[Python-checkins] commit of r41838 - in python/trunk/Lib: csv.py test/test_csv.py

skip.montanaro python-checkins at python.org
Wed Dec 28 16:37:29 CET 2005


Author: skip.montanaro
Date: Wed Dec 28 16:37:25 2005
New Revision: 41838

Modified:
   python/trunk/Lib/csv.py
   python/trunk/Lib/test/test_csv.py
Log:
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.


Modified: python/trunk/Lib/csv.py
==============================================================================
--- python/trunk/Lib/csv.py	(original)
+++ python/trunk/Lib/csv.py	Wed Dec 28 16:37:25 2005
@@ -271,7 +271,7 @@
                 for char in ascii:
                     metaFrequency = charFrequency.get(char, {})
                     # must count even if frequency is 0
-                    freq = line.strip().count(char)
+                    freq = line.count(char)
                     # value is the mode
                     metaFrequency[freq] = metaFrequency.get(freq, 0) + 1
                     charFrequency[char] = metaFrequency

Modified: python/trunk/Lib/test/test_csv.py
==============================================================================
--- python/trunk/Lib/test/test_csv.py	(original)
+++ python/trunk/Lib/test/test_csv.py	Wed Dec 28 16:37:25 2005
@@ -836,7 +836,6 @@
 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow'
 'Stonecutters Seafood and Chop House':'Lemont':'IL':'12/19/02':'Week Back'
 """
-
     header = '''\
 "venue","city","state","date","performers"
 '''
@@ -852,6 +851,8 @@
 47483648;43.0;170;abc;def
 '''
 
+    sample5 = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n"
+
     def test_has_header(self):
         sniffer = csv.Sniffer()
         self.assertEqual(sniffer.has_header(self.sample1), False)
@@ -879,6 +880,8 @@
         self.assertEqual(dialect.delimiter, "/")
         dialect = sniffer.sniff(self.sample4)
         self.assertEqual(dialect.delimiter, ";")
+        dialect = sniffer.sniff(self.sample5)
+        self.assertEqual(dialect.delimiter, "\t")
 
 if not hasattr(sys, "gettotalrefcount"):
     if test_support.verbose: print "*** skipping leakage tests ***"


More information about the Python-checkins mailing list