]> granicus.if.org Git - python/commitdiff
[2.7] bpo-30157: Fix csv.Sniffer.sniff() regex pattern. (GH-5601) (GH-5604)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 9 Feb 2018 22:02:04 +0000 (00:02 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Feb 2018 22:02:04 +0000 (00:02 +0200)
Co-authored-by: Jake Davis <jcdavis@awedge.net>.
(cherry picked from commit 2411292ba8155327125d8a1da8a4c9fa003d5909)

Lib/csv.py
Lib/test/test_csv.py
Misc/ACKS
Misc/NEWS.d/next/Library/2018-02-09-14-44-43.bpo-30157.lEiiAK.rst [new file with mode: 0644]

index c155ada794a606f26fb105d8227d100bbd986e1a..70c53ae7791e34a33ea1b276d4d9741d187d8a12 100644 (file)
@@ -217,7 +217,7 @@ class Sniffer:
         matches = []
         for restr in ('(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
                       '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)',   #  ".*?",
-                      '(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)',  # ,".*?"
+                      '(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)',   # ,".*?"
                       '(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'):                            #  ".*?" (no delim, no space)
             regexp = re.compile(restr, re.DOTALL | re.MULTILINE)
             matches = regexp.findall(data)
index d4567590cb1d8e4289352f73d1cb0e80abd59673..88d5af7c130d5dbac4f57747d612769fad26b994 100644 (file)
@@ -1036,6 +1036,15 @@ Stonecutters Seafood and Chop House+ Lemont+ IL+ 12/19/02+ Week Back
         self.assertEqual(sniffer.has_header(self.header2 + self.sample8),
                          True)
 
+    def test_guess_quote_and_delimiter(self):
+        sniffer = csv.Sniffer()
+        for header in (";'123;4';", "'123;4';", ";'123;4'", "'123;4'"):
+            dialect = sniffer.sniff(header, ",;")
+            self.assertEqual(dialect.delimiter, ';')
+            self.assertEqual(dialect.quotechar, "'")
+            self.assertIs(dialect.doublequote, False)
+            self.assertIs(dialect.skipinitialspace, False)
+
     def test_sniff(self):
         sniffer = csv.Sniffer()
         dialect = sniffer.sniff(self.sample1)
index dc64293b69652b199b58d5ee63e9d142d5afe72d..f0a115d5d555596ffb4efcfa46cd3b7614fbd0c9 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -317,6 +317,7 @@ Kushal Das
 Jonathan Dasteel
 Pierre-Yves David
 A. Jesse Jiryu Davis
+Jake Davis
 Merlijn van Deen
 John DeGood
 Ned Deily
diff --git a/Misc/NEWS.d/next/Library/2018-02-09-14-44-43.bpo-30157.lEiiAK.rst b/Misc/NEWS.d/next/Library/2018-02-09-14-44-43.bpo-30157.lEiiAK.rst
new file mode 100644 (file)
index 0000000..9f65193
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed guessing quote and delimiter in csv.Sniffer.sniff() when only the last
+field is quoted.  Patch by Jake Davis.