Python 删除斜杠问题

Python 删除斜杠问题,python,python-2.x,Python,Python 2.x,我正试图压缩/改进/优化这段代码 cleaned = stringwithslashes cleaned = cleaned.replace("\\n", "\n") cleaned = cleaned.replace("\\r", "\n") cleaned = cleaned.replace("\\", "") 但是上面的代码不能正常工作试试这个 cleaned = stringwithslashes.decode('string_escape') 首先,您的代码缺少引号。你试过这个吗

我正试图压缩/改进/优化这段代码

cleaned = stringwithslashes
cleaned = cleaned.replace("\\n", "\n")
cleaned = cleaned.replace("\\r", "\n")
cleaned = cleaned.replace("\\", "")
但是上面的代码不能正常工作

试试这个

cleaned = stringwithslashes.decode('string_escape')

首先,您的代码缺少引号。你试过这个吗

cleaned = stringwithslashes
cleaned = cleaned.replace("\\n", "\n")
cleaned = cleaned.replace("\\r", "\n")
cleaned = cleaned.replace("\\", "")

Uhhh,为什么要用PHP标记呢?这不是PHP,除非它是某种模板语言……当你想要不可替换的反斜杠时,使用
r'raw strings'
。第2行和第3行有语法错误(没有结束引号?)呃,什么?引用此项?这仅适用于Python 2。对于Python3,它不再工作了。