Skip to content

An escape error of re.py (re.error: bad escape \s at position 0) in "utils/logloader.py" based on Python3.7 #50

Closed

Description

@DawNIng-github

Dear authors,
Thanks for your work. When I ran the file, "MoLFI_demo.py", based on Python3.7, I got the error as following.

Traceback (most recent call last):
  File "~/anaconda3/envs/tf/lib/python3.7/sre_parse.py", line 1021, in parse_template
    this = chr(ESCAPES[this][1])
KeyError: '\\s'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "MoLFI_demo.py", line 14, in <module>
    parser.parse(log_file)
  File "../logparser/MoLFI/MoLFI.py", line 41, in parse
    loader = logloader.LogLoader(self.log_format, self.n_workers)
  File "../logparser/utils/logloader.py", line 38, in __init__
    self.headers, self.regex = self._generate_logformat_regex(self.logformat)
  File "../logparser/utils/logloader.py", line 79, in _generate_logformat_regex
    splitter = re.sub(' +', '\s+', splitters[k])
  File "~/anaconda3/envs/tf/lib/python3.7/re.py", line 192, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "~/anaconda3/envs/tf/lib/python3.7/re.py", line 309, in _subx
    template = _compile_repl(template, pattern)
  File "~/anaconda3/envs/tf/lib/python3.7/re.py", line 300, in _compile_repl
    return sre_parse.parse_template(repl, pattern)
  File "~/anaconda3/envs/tf/lib/python3.7/sre_parse.py", line 1024, in parse_template
    raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \s at position 0

I'd be grateful if you could help me.

Activity

  1. zhujiem commented on Oct 11, 2019

    @zhujiem
    Member

    Please use Python 3.6

  2. ankit-nassa commented on Oct 11, 2019

    @ankit-nassa

    You can also try replacing import re to import regex as re

  3. fuyunliu commented on Oct 14, 2019

    @fuyunliu

    template_regex = re.sub(r'\\ +', r'\s+', template_regex)

    what do you mean sub \\ + with \s+ ?

  4. shoaib-intro commented on May 9, 2022

    @shoaib-intro

    template_regex = re.sub(r'\\ +', r'\s+', template_regex)

    what do you mean sub \\ + with \s+ ?
    Hi @fuyunliu
    I faced same error bad escape sequence error at this line have you solved it?

  5. devharsh commented on May 27, 2022

    @devharsh

    Traceback (most recent call last):

    File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 1041, in parse_template
    this = chr(ESCAPES[this][1])

    KeyError: '\s'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):

    File "/usr/local/lib/python3.9/site-packages/spyder_kernels/py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

    File "/Users/devharsh/Downloads/Drain_demo.py", line 28, in
    parser.parse(log_file)

    File "/Users/devharsh/Downloads/Drain.py", line 293, in parse
    self.outputResult(logCluL)

    File "/Users/devharsh/Downloads/Drain.py", line 224, in outputResult
    self.df_log["ParameterList"] = self.df_log.apply(self.get_parameter_list, axis=1)

    File "/usr/local/lib/python3.9/site-packages/pandas/core/frame.py", line 8839, in apply
    return op.apply().finalize(self, method="apply")

    File "/usr/local/lib/python3.9/site-packages/pandas/core/apply.py", line 727, in apply
    return self.apply_standard()

    File "/usr/local/lib/python3.9/site-packages/pandas/core/apply.py", line 851, in apply_standard
    results, res_index = self.apply_series_generator()

    File "/usr/local/lib/python3.9/site-packages/pandas/core/apply.py", line 867, in apply_series_generator
    results[i] = self.f(v)

    File "/Users/devharsh/Downloads/Drain.py", line 347, in get_parameter_list
    template_regex = re.sub(r'\ +', r'\s+', template_regex)

    File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)

    File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 327, in _subx
    template = _compile_repl(template, pattern)

    File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 318, in _compile_repl
    return sre_parse.parse_template(repl, pattern)

    File "/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 1044, in parse_template
    raise s.error('bad escape %s' % this, len(this))

    error: bad escape \s

  6. devharsh commented on May 28, 2022

    @devharsh

    error when I changed import re to import regex as re

    Traceback (most recent call last):

    File "/usr/local/lib/python3.9/site-packages/spyder_kernels/py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

    File "/Users/devharsh/Downloads/Drain_demo.py", line 28, in
    parser.parse(log_file)

    File "/Users/devharsh/Downloads/Drain.py", line 293, in parse
    self.outputResult(logCluL)

    File "/Users/devharsh/Downloads/Drain.py", line 224, in outputResult
    self.df_log["ParameterList"] = self.df_log.apply(self.get_parameter_list, axis=1)

    File "/usr/local/lib/python3.9/site-packages/pandas/core/frame.py", line 8839, in apply
    return op.apply().finalize(self, method="apply")

    File "/usr/local/lib/python3.9/site-packages/pandas/core/apply.py", line 727, in apply
    return self.apply_standard()

    File "/usr/local/lib/python3.9/site-packages/pandas/core/apply.py", line 851, in apply_standard
    results, res_index = self.apply_series_generator()

    File "/usr/local/lib/python3.9/site-packages/pandas/core/apply.py", line 867, in apply_series_generator
    results[i] = self.f(v)

    File "/Users/devharsh/Downloads/Drain.py", line 347, in get_parameter_list
    template_regex = re.sub(r'\ +', r'\s+', template_regex)

    File "/usr/local/lib/python3.9/site-packages/regex/regex.py", line 278, in sub
    return pat.sub(repl, string, count, pos, endpos, concurrent, timeout)

    File "/usr/local/lib/python3.9/site-packages/regex/regex.py", line 700, in _compile_replacement_helper
    is_group, items = _compile_replacement(source, pattern, is_unicode)

    File "/usr/local/lib/python3.9/site-packages/regex/_regex_core.py", line 1737, in _compile_replacement
    raise error("bad escape \%s" % ch, source.string, source.pos)

    error: bad escape \s

  7. estebanpw commented on Aug 3, 2022

    @estebanpw

    Just adding this in case someone else has this problem in 2022 when using python 3.8.10.
    I changed the following line:

    splitter = re.sub(' +', '\s+', splitters[k])

    to

    splitter = re.sub(' +', '\\\s+', splitters[k])

    and that fixed it. Note that I changed this only in the function def _generate_logformat_regex(self, logformat): of the file Spell/Spell.py (because I was using spell) and not in utils/logloader.py.

  8. Jun-jie-Huang commented on Feb 24, 2023

    @Jun-jie-Huang

    You can also try replacing import re to import regex as re

    Try to degrade regex pip install regex=2022.3.2 if it still raises exceptions. This works for me.

  9. khawar-islam commented on Jun 7, 2023

    @khawar-islam

    Hello @estebanpw I implemented your suggestion but still the problem is same. Any idea how to fix?

  10. shoaib-intro commented on Jun 7, 2023

    @shoaib-intro

    Hello @estebanpw I implemented your suggestion but still the problem is same. Any idea how to fix?

    also you can try import regex as re if import throw error use pip install regex but keep @estebanpw Suggestion also this is additional to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions