例如,您如何以编程方式将 Python 3 字符串变量中的 \" 转换为 \\"。
str1 = """
"this is a string \"and this is whatever\" and then some"
"""
这些是我没有成功的尝试:
str2 = re.sub('\\"', '\\\\', str1)
print('Attempt #1) ' + str2)
and
str3 = str1.replace('\"', '\\"')
print ('Attempt #2) ' + str3)
这两种尝试都没有产生我想要做的事情,我试图以编程方式输出的输出是:
“这是一个字符串 \\”,这是任何东西\\“然后是一些”
答案 0 :(得分:3)
你需要这样做
import Moment from "react-moment";
使用 'r' 将字符串转换为原始字符串,这样可以防止转义序列解释。
答案 1 :(得分:0)
代码是这样的:
print("this is a string \\\\\" and this is whatever\\\\\" and then some")
每个 \
需要两个反斜杠,另一个反斜杠才能得到 "