summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-08-03 15:05:13 -0600
committerGitHub <noreply@github.com>2021-08-03 15:05:13 -0600
commitb5cdbf0434019e802b5e1a44bee06e67e0beef75 (patch)
tree89ee7db033dcfdafa15cecb0dd342a023736b64f /numpy/lib/npyio.py
parent2a9a95d250c91f842b9fdc32cbeaeaced8b5896b (diff)
parent60fd08679dda9f690e6548bf5ec77e66e1f6dd68 (diff)
downloadnumpy-b5cdbf0434019e802b5e1a44bee06e67e0beef75.tar.gz
Merge pull request #19601 from anntzer/loadtxtcomments
PERF: Speedup comments handling in loadtxt.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py10
1 files changed, 4 insertions, 6 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 785149f56..225b22a5c 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -973,9 +973,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
def split_line(line):
"""Chop off comments, strip, and split at delimiter. """
line = _decode_line(line, encoding=encoding)
-
- if comments is not None:
- line = regex_comments.split(line, maxsplit=1)[0]
+ for comment in comments: # Much faster than using a single regex.
+ line = line.split(comment, 1)[0]
line = line.strip('\r\n')
return line.split(delimiter) if line else []
@@ -1030,9 +1029,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
if isinstance(comments, (str, bytes)):
comments = [comments]
comments = [_decode_line(x) for x in comments]
- # Compile regex for comments beforehand
- comments = (re.escape(comment) for comment in comments)
- regex_comments = re.compile('|'.join(comments))
+ else:
+ comments = []
if delimiter is not None:
delimiter = _decode_line(delimiter)