0

I'm working with some text that have this format:

"Say that they\'ll price match" "wouldn't price match" "hey don\\'t install on weekends"

The problem I have here is that I have this backslash '\' '\\' all over the text, I need to remove those, using

.replace('\\','')

But I'm not sure if those are the unique symbols that appears on the text or if there are more combinations, I can't read all the data manually to check this, there are thousands of pages. Is this a common problem? is caused by an specific encoding format?

Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

2 Answers2

1

Almost a dupe of these questions.

When you see a double backslash \\, that's Python's way of representing a literal slash.

>>> s = 'some s\\tring'
>>> t
'vertical \tab'
>>> s           # just `s` gives the repr
'some s\\tring'
>>> print a     # prints `s`
some s\tring
>>> # ^^^ notice the lack of slashes or quotes above

In 'some s\\tring', the \\ implies a single actual \. Without it, the backslash \ would apply to t which represents a vertical tab \t.

The statement .replace('\\','') tries to replace actual slashes with nothing. But what you have is not an actual slash, it's the "escape sequence" for a single quote. Using tab as an example:

>>> t = 'vertical \tab'  # see what I did there?
>>> print t
vertical        ab
>>> # notice the actual tab above
...
>>> t.replace('\\', '')  # trying to remove literal slash
'vertical \tab'
>>> # that didn't work
...
>>> s.replace('\\', '')  # try it on the string above which has an actual slash
'some string'
Community
  • 1
  • 1
aneroid
  • 12,983
  • 3
  • 36
  • 66
0

In shell scripting a backslash is used to escape a character.That is, it allows the shell not to interpret the character as a special character . Single quotes and double quotes are special characters in a shell and it is used to enclosed a group of characters to form a string...so if you escape a quote with a backslash the shell will interpret it as a literal quote instead of interpreting it as a special character.

I am assuming here that the output you are seeing is not sent to a shell that is why you are seeing literal backslashes followed by a quote?

repzero
  • 8,254
  • 2
  • 18
  • 40