-1

I have the below sentence, where I want to remove the "\" from the word "don'\t". in order to be able to replace don't with do not.

a= "To say, "don\'t hate foot" is as dumb as saying "don\'t"
my_dict ={"don't":'do not'}

print(a.replace(r'\\', ""))
out: To say, "don\\\'t hate foot" is as dumb as saying "don\\\'t

print(re.sub(r'\\', "",a))
out:  To say, "don\'t hate foot" is as dumb as saying "don\'t

but I would like:

To say, "don't hate foot" is as dumb as saying "don't

in order to apply

' '.join([my_dict.get(word, word) for word in x.split()]))

I am cleaning a lot of sentences on one go so I would like something stable that will not affect other syntax

A.Papa
  • 486
  • 2
  • 8
  • 20
  • 4
    The output you want to get is a syntax error. – Aran-Fey Apr 01 '18 at 20:38
  • drop the raw prefix: `a = 'To say, "don\'t hate foot" is as dumb as saying "don\'t'` and try to print the string. – Jean-François Fabre Apr 01 '18 at 20:39
  • What to you mean Aran? Should I remove " first? The text is coming from tweets and facebook... if of any help – A.Papa Apr 01 '18 at 20:39
  • 2
    I think you may be confusing what’s in the strings with the repr of those strings. The repr is meant to be something you can type back in as source code to get those same contents, not the contents themselves. Try printing them out with `print` to see what’s actually there without quotes and escaping. – abarnert Apr 01 '18 at 20:40
  • Why do you write `r'\\'`? You should just write `'\\'`. – Mateen Ulhaq Apr 01 '18 at 20:42
  • @abarnert what is the repr? Could you give an example? – A.Papa Apr 01 '18 at 20:42
  • @MateenUlhaq I saw it in a previous stack post https://stackoverflow.com/questions/6486918/replace-all-with-python – A.Papa Apr 01 '18 at 20:43
  • it's nice to try to create a [mcve], but in this case, since the text is coming from tweets & facebook, creating an example with raw strings literals doesn't help. – Jean-François Fabre Apr 01 '18 at 20:44
  • [Difference between `__str__` and `__repr__` in Python](https://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python/2626364#2626364) – abarnert Apr 01 '18 at 20:45
  • If you just evaluate an expression at the interactive interpreter, you get the `repr`. So you're already _seeing_ examples of that. If you `print` something, you get the `str`. Try calling `print(r.sub(…))` and so on to see examples. – abarnert Apr 01 '18 at 20:47

2 Answers2

1

When you type python from the command line you enter into the REPL (Read, Evaluate, Print, Loop). One of the behaviors of this mode is to return the result of the last command. This appears like printing, but it is not returning the string representation, instead, it is returning a valid syntax that would evaluate to the returned expression. This means for strings, the output is a string with special characters escaped (and surrounded with single quotes). If you want to see the contents outputted, you can surround it with the python function print().

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • thank you for the explanation, however, I am not sure why I cannot clean the example posted initially. – A.Papa Apr 01 '18 at 22:14
0

ok I found the solution. My apologies as my question was not straightforward. the ultimate goal was to replace don/'t with do not. (see update)

So first I need to remove r'"' from the sentence and then replace don't with do not..

a.replace(r'"', ""))

' '.join([my_dict.get(word, word) for word in a.split()]))

where

my_dict = {"don't":'do not'}

please advice if I should remove the post. Thank you for your efforts to explain me what I was doing wrong.

A.Papa
  • 486
  • 2
  • 8
  • 20