]> granicus.if.org Git - python/commitdiff
[3.6] bpo-30157: Fix csv.Sniffer.sniff() regex pattern. (GH-5601) (GH-5603)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 9 Feb 2018 22:01:40 +0000 (00:01 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Feb 2018 22:01:40 +0000 (00:01 +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 0349e0bd1162c4bd6bc7ea3a387e7fbee2e7d873..a05c8da1b2c883aed246f25b06b20fe047129630 100644 (file)
@@ -217,7 +217,7 @@ class Sniffer:
         matches = []
         for restr in (r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
                       r'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)',   #  ".*?",
-                      r'(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)',  # ,".*?"
+                      r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)',   # ,".*?"
                       r'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'):                            #  ".*?" (no delim, no space)
             regexp = re.compile(restr, re.DOTALL | re.MULTILINE)
             matches = regexp.findall(data)
index fe248019a0f65af50bfd56fc03b1c650820438d3..b65cbf6a50e3ec32d6ff40eb8037b80b47fe6424 100644 (file)
@@ -986,6 +986,16 @@ 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'"):
+            with self.subTest(header):
+                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 ac1345938cb4c03f3b14993ad8205f7a5d7c689c..0e64609df64e156fac819ea561602482e9ce1eb0 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -350,6 +350,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.