Remove backslash from string

I have a string that is a sentence like I don't want it, there'll be others

So the text looks like this: I don\'t want it, there\'ll be other

for some reason a \comes with the text next to '. This has been read from another source. I want to delete it, but I can’t. I tried. sentence.replace("\'","'")

sentence.replace(r"\'","'")

sentence.replace("\\","")

sentence.replace(r"\\","")

sentence.replace(r"\\\\","")

I know I have \to avoid something, so I'm not sure how to do this with quotation marks

+4
source share
4 answers

\is there escape ' . This is only visible in the reprstring representation ( ), it is not a character in the string. See Next Demo

>>> repr("I don't want it, there'll be others")
'"I don\'t want it, there\'ll be others"'

>>> print("I don't want it, there'll be others")
I don't want it, there'll be others
+8
source

Try using:

sentence.replace("\\", "")

, escape-, - , .

0

Better use regex to remove backslashes:

>>> re.sub(u"u\005c'", r"'", "I don\'t want it, there\'ll be other")
"I don't want it, there'll be other"
0
source

If your text comes from workaround text and you did not clear it by undoing it before processing it using the NLP tools, you can easily undo HTML markup, for example:

In python2.x:

>>> import sys; sys.version
'2.7.6 (default, Jun 22 2015, 17:58:13) \n[GCC 4.8.2]'
>>> import HTMLParser
>>> txt = """I don\'t want it, there\'ll be other"""
>>> HTMLParser.HTMLParser().unescape(txt)
"I don't want it, there'll be other"

In python3:

>>> import sys; sys.version
'3.4.0 (default, Jun 19 2015, 14:20:21) \n[GCC 4.8.2]'
>>> import html
>>> txt = """I don\'t want it, there\'ll be other"""
>>> html.unescape(txt)
"I don't want it, there'll be other"

See also: How to remove HTML objects in a string in Python 3.1?

0
source

Source: https://habr.com/ru/post/1611933/


All Articles