2012-03-09 55 views
0

我试图凝聚/改善/优化该代码的stripslashes问题

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

,但上面的代码不能正常工作

+2

呃,为什么这个标记为PHP? – animuson 2012-03-09 05:33:44

+0

这不是PHP,除非它的一些模板语言... – prodigitalson 2012-03-09 05:34:24

+0

当你想要非转义的反斜杠时使用'r'raw strings'。并且第2,3行有语法错误(没有收盘引号?) – wim 2012-03-09 05:42:07

回答

0

试试这个

cleaned = stringwithslashes.decode('string_escape') 
+0

错误,什么?参考这个? – 2012-03-09 05:35:09

+0

这仅适用于Python 2.使用Python 3时,它不再适用。 – nerdoc 2017-05-08 20:07:30

1

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

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