转义Python 3.7中的所有反斜杠 解码escape编码的文本

Published on Aug. 22, 2023, 12:06 p.m.

2021-07-07 记事

使用内置函数repr()转义Python 3.7中的所有反斜杠\。*

repr('your_string')

检查链接: https://docs.python.org/3/library/functions.html#repr


import re

a="""

Hello, world!

Hello, world!

Hello, world!

“
<ul>
<li>"</li>
</ul>
"

Hello, world!

"""

s=re.escape(a)

r = re.compile(r"\(.)") # Note the parethesis, that's a capturing group

r.sub(r'\1', s).replace("\",''),s # \1 means: replace with value of first capturing group


输出如下

(‘\n\nHello, world!\n\nHello, world!\n\nHello, world!\n“ \n- “\n”\nHello, world!\n\n\n’, ‘\n\nHello,\ world!\n\nHello,\ world!\n\nHello,\ world!\n“\ \ \n-\ “\n”\nHello,\ world!\n\n\n’)

Tags: