summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-02-09 18:00:49 (GMT)
committerGitHub <noreply@github.com>2018-02-09 18:00:49 (GMT)
commit2411292ba8155327125d8a1da8a4c9fa003d5909 (patch)
tree7e5ca5d6b4151fc38ddf4324d67edbc4be630499 /Lib
parentbfe4fd5f2e96e72eecb5b8a0c7df0ac1689f3b7e (diff)
downloadcpython-2411292ba8155327125d8a1da8a4c9fa003d5909.zip
cpython-2411292ba8155327125d8a1da8a4c9fa003d5909.tar.gz
cpython-2411292ba8155327125d8a1da8a4c9fa003d5909.tar.bz2
bpo-30157: Fix csv.Sniffer.sniff() regex pattern. (GH-5601)
Co-authored-by: Jake Davis <jcdavis@awedge.net>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/csv.py2
-rw-r--r--Lib/test/test_csv.py10
2 files changed, 11 insertions, 1 deletions
diff --git a/Lib/csv.py b/Lib/csv.py
index 6a85876..58624af 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -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)
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index fe24801..b65cbf6 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -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)