question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
ankit-nassacommented, Oct 11, 2019

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

0reactions
estebanpwcommented, Aug 3, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python 3.7.4: 're.error: bad escape \s at position 0'
into a regular string. I checked the Python 3.7 documentation, and it appears that \s is still the escape sequence for white space....
Read more >
Gotchas - Python re(gex)? - learnbyexample
Example based guide to mastering Python regular expressions. ... re.sub(r',', r'\x7c', '1,2') re.error: bad escape \x at position 0 >>> re.sub(r',', ...
Read more >
Issue 47023: re.sub shows key error on regex escape chars ...
error ('bad escape %s' % this, len(this))" should probably be "from None", since as @mrabarnett notes the KeyError is an implementation detail.
Read more >
Python Re Escape - Finxter
Challenge: Some characters have a special meaning in Python strings and regular expressions. Say you want to to search for string "(s)" but...
Read more >
[Example code]-How to fix bad escape regex error (python re)
I would try to achieve to convert that date to 05/26/2012. I tried to achieve this without using DateTime but with regex. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found