striplashes问题

时间:2012-03-09 05:29:54

标签: python python-2.x

我正在尝试压缩/改进/优化此代码

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

但上面的代码无法正常工作

2 个答案:

答案 0 :(得分:1)

对于初学者,您的代码缺少引号。你试过这个吗?

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

答案 1 :(得分:0)

试试这个

cleaned = stringwithslashes.decode('string_escape')