Skip to content

Commit f1ebd08

Browse files
Do not skip spaces after a space delimiter
They cannot be a padding, and skipping them rescans every run of spaces, which makes the search quadratic.
1 parent ddb0488 commit f1ebd08

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Lib/csv.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,12 @@ def _guess_quote_and_delimiter(self, data, delimiters):
336336
doublequote = False
337337
if delim:
338338
dq_regexp = re.compile(
339-
r"(?:(?<=%(delim)s)|^) *+%(quote)s" # ,"
339+
r"(?:(?<=%(delim)s)|^)%(space)s%(quote)s" # ,"
340340
r"((?:%(quote)s%(quote)s|[^%(quote)s]++)*+)" # the body
341341
r"%(quote)s(?:%(delim)s|$)" # ",
342-
% {'delim': re.escape(delim), 'quote': quotechar},
342+
% {'delim': re.escape(delim), 'quote': quotechar,
343+
# Skipping spaces after a space rescans them.
344+
'space': ' *+' if delim != ' ' else ''},
343345
re.MULTILINE)
344346
dquotechar = quotechar * 2
345347
doublequote = any(dquotechar in m[1]

0 commit comments

Comments
 (0)