I have the following string and I'm trying to figure out the best practice for unescaping it.
The solution has to be somewhat flexible in that I'm receiving this input from an API and I can't be absolutely certain that the current character structure (\n
as opposed to \r
) will always be the same.
'"If it ain\'t broke, don\'t fix it." \nWent in for a detailed car wash.\nThe attendants raved-up my engine when taking the car into
the tunnel. NOTE: my car is...'
This regex seems like it should work:
text_excerpt = re.sub(r'[\s"\\]', ' ', raw_text_excerpt).strip()
I've aso read that decode()
might work (and would be a better solution generally).
raw_text_excerpt.decode('string_unescape')
Tried something along those lines and it didn't work. Any suggestions? Is regex best here?