清除 python 中的无效转义?

Clear invalid escape in python?

在Python中,我有一个字符串:

a = "\s"

在 JavaScript 中,a 将是单个字母 "s",但在 Python 中,a 将是 "\s"

如何使 Python 在这种情况下表现得与 JavaScript 相同?


实际情况可能更复杂:a = "<div class=\"haha\"><\/div>",在这种情况下,JavaScript 正确 HTML 但 python 失败

假设没有 encoding/decoding 正在发生?

a == r"\s"吗?

您可以简单地:

a.replace('\','')

示例:

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

参见:

  • 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?