-2

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.

  • 4
    `\s` is for _matching_ whitespace, not creating it; you need to use actual space characters. Or do `mystring.replace('\t', ' ')`. – snakecharmerb Jun 09 '22 at 14:08
  • Does this answer your question? [Is there a simple way to remove multiple spaces in a string?](https://stackoverflow.com/q/1546226/6045800) Just change the `' +'` to a `'\s+'` – Tomerikoo Jun 09 '22 at 14:13
  • 1
    Further, the digraph `\t` has no special meaning to the regular-expression engine: it's just the letter `t`. You *don't* want to use a raw string, because you *want* to pass a literal tab character as the regular expression to match. – chepner Jun 09 '22 at 14:13
  • Or https://stackoverflow.com/questions/54785152/replace-tab-with-space-in-entire-text-file-python – Wiktor Stribiżew Jun 09 '22 at 14:13
  • problem is not finding a way to replace, but how to do it using regex functionality, thanks for your answers – azizalizada Jun 09 '22 at 15:21

1 Answers1

0
import re

text = "London\tis the\tcapital of Great\tBritain"

x = re.sub(r"\t",r" ", text)
dresende
  • 2,211
  • 1
  • 16
  • 25
Jh123
  • 83
  • 8