[Python-checkins] bpo-30157: Fix csv.Sniffer.sniff() regex pattern. (GH-5601) (GH-5602)

Serhiy Storchaka webhook-mailer at python.org
Fri Feb 9 17:00:56 EST 2018


https://github.com/python/cpython/commit/2ef69a1d45de8aa41c45d32d9ee1ff227bb1a566
commit: 2ef69a1d45de8aa41c45d32d9ee1ff227bb1a566
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2018-02-10T00:00:48+02:00
summary:

bpo-30157: Fix csv.Sniffer.sniff() regex pattern. (GH-5601) (GH-5602)

Co-authored-by: Jake Davis <jcdavis at awedge.net>
(cherry picked from commit 2411292ba8155327125d8a1da8a4c9fa003d5909)

files:
A Misc/NEWS.d/next/Library/2018-02-09-14-44-43.bpo-30157.lEiiAK.rst
M Lib/csv.py
M Lib/test/test_csv.py
M Misc/ACKS

diff --git a/Lib/csv.py b/Lib/csv.py
index 6a8587674fe0..58624af90534 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -217,7 +217,7 @@ def _guess_quote_and_delimiter(self, data, delimiters):
         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)
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index fe248019a0f6..b65cbf6a50e3 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -986,6 +986,16 @@ def test_has_header_regex_special_delimiter(self):
         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)
diff --git a/Misc/ACKS b/Misc/ACKS
index 4c8b86ac45d4..b31190ca5283 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -355,6 +355,7 @@ Kushal Das
 Jonathan Dasteel
 Pierre-Yves David
 A. Jesse Jiryu Davis
+Jake Davis
 Ratnadeep Debnath
 Merlijn van Deen
 John DeGood
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
index 000000000000..9f651930ac2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-02-09-14-44-43.bpo-30157.lEiiAK.rst
@@ -0,0 +1,2 @@
+Fixed guessing quote and delimiter in csv.Sniffer.sniff() when only the last
+field is quoted.  Patch by Jake Davis.



More information about the Python-checkins mailing list