I am asked to replace tab spaces with whitespace using regex in python. Running the code below :
text = "London is the capital of Great Britain"
x = re.sub(r"\t",r"\s", text)
I get error, which says:
KeyError Traceback (most recent call last)
~\anaconda3\lib\sre_parse.py in parse_template(source, state)
1038 try:
-> 1039 this = chr(ESCAPES[this][1])
1040 except KeyError:
KeyError: '\\s'
During handling of the above exception, another exception occurred:
error Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_600/989794690.py in <module>
1 text = "London is the capital of Great Britain"
----> 2 x = re.sub(r"\t",r"\s", text)
~\anaconda3\lib\re.py in sub(pattern, repl, string, count, flags)
208 a callable, it's passed the Match object and must return
209 a replacement string to be used."""
--> 210 return _compile(pattern, flags).sub(repl, string, count)
211
212 def subn(pattern, repl, string, count=0, flags=0):
~\anaconda3\lib\re.py in _subx(pattern, template)
325 def _subx(pattern, template):
326 # internal: Pattern.sub/subn implementation helper
--> 327 template = _compile_repl(template, pattern)
328 if not template[0] and len(template[1]) == 1:
329 # literal replacement
~\anaconda3\lib\re.py in _compile_repl(repl, pattern)
316 def _compile_repl(repl, pattern):
317 # internal: compile replacement pattern
--> 318 return sre_parse.parse_template(repl, pattern)
319
320 def _expand(pattern, match, template):
~\anaconda3\lib\sre_parse.py in parse_template(source, state)
1040 except KeyError:
1041 if c in ASCIILETTERS:
-> 1042 raise s.error('bad escape %s' % this, len(this))
1043 lappend(this)
1044 else:
error: bad escape \s at position 0
Can anyone help me with this issue? I am using python 3 and jupyter notebook.