Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear invalid escape in python?

In Python, I have a string:

a = "\s"

In JavaScript, a would be the single letter "s", but in Python, a would be "\s".

How can I make Python behave the same way as JavaScript in this situation?


the real case may be more complicate : a = "<div class=\"haha\"><\/div>" , In this case , JavaScript get right HTML but python failed
like image 297
eliriclzj Avatar asked Mar 15 '26 00:03

eliriclzj


1 Answers

Assuming that there are no encoding/decoding that is happening?

Is a == r"\s" ?

You could simply:

a.replace('\\','')

example:

>>> a = "<div class=\"haha\"><\/div>"
>>> a.replace('\\','')
'<div class="haha"></div>'

See:

  • What exactly do "u" and "r" string flags do in Python, and what are raw string literals?
  • Decode HTML entities in Python string?
  • Process escape sequences in a string in Python
  • What is the difference between encode/decode?
like image 131
jmunsch Avatar answered Mar 16 '26 14:03

jmunsch