Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DawNIng-github opened this issue Oct 11, 2019 · 10 comments

Comments

@DawNIng-github
Copy link

DawNIng-github commented Oct 11, 2019

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.

@zhujiem
Copy link
Member

zhujiem commented Oct 11, 2019

Please use Python 3.6

@ankit-nassa
Copy link

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

@fuyunliu
Copy link

fuyunliu commented Oct 14, 2019

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

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

@zhujiem zhujiem closed this as completed Oct 30, 2019
@shoaib-intro
Copy link

shoaib-intro commented May 9, 2022

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?

@devharsh
Copy link

Traceback (most recent call last):

File "/usr/local/Cellar/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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

@devharsh
Copy link

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

@estebanpw
Copy link

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.

@Jun-jie-Huang
Copy link

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.

@khawar-islam
Copy link

khawar-islam commented Jun 7, 2023

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

@shoaib-intro
Copy link

shoaib-intro commented Jun 7, 2023

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.

xpai pushed a commit that referenced this issue Sep 5, 2023
xpai pushed a commit that referenced this issue Sep 5, 2023
xpai pushed a commit that referenced this issue Sep 5, 2023
xpai pushed a commit that referenced this issue Sep 5, 2023
* Fix PRs: merge #86, merge #85, merge #83, merge #80, merge #78, merge #65, merge #57, merge #53, merge #52, merge #51, merge #49
* Fix issues: #98, #79, #72, #84, #75, #71, #69, #56, #50, #45, #16, #82
xpai pushed a commit that referenced this issue Sep 5, 2023
xpai pushed a commit that referenced this issue Sep 5, 2023
* Close PRs: close #86, #85, #83, #80, #78, #65, #57, #53, #52, #51, #49
* Fix issues: fix #98, #79, #72, #84, #75, #71, #69, #56, #50, #45, #16, #82
* Release pypi package wheel
xpai pushed a commit that referenced this issue Sep 5, 2023
* Close PRs: close #86, #85, #83, #80, #78, #65, #57, #53, #52, #51, #49
* Fix issues: fix #98, #79, #72, #84, #75, #71, #69, #56, #50, #45, #16, #82
* Release package wheel to pypi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants