diff --git a/Lib/csv.py b/Lib/csv.py index c155ada794a..70c53ae7791 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -217,7 +217,7 @@ class Sniffer: matches = [] for restr in ('(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?P=delim)', # ,".*?", '(?:^|\n)(?P["\']).*?(?P=quote)(?P[^\w\n"\'])(?P ?)', # ".*?", - '(?P>[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?:$|\n)', # ,".*?" + '(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?:$|\n)', # ,".*?" '(?:^|\n)(?P["\']).*?(?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 d4567590cb1..88d5af7c130 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -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) diff --git a/Misc/ACKS b/Misc/ACKS index dc64293b696..f0a115d5d55 100644 --- 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 index 00000000000..9f651930ac2 --- /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.