我写了一段代码,将PHP的条纹转换为有效的Python反斜杠转义:
cleaned = stringwithslashes
cleaned = cleaned.replace('\\n', '\n')
cleaned = cleaned.replace('\\r', '\n')
cleaned = cleaned.replace('\\', '')我该如何压缩它呢?
发布于 2008-08-17 12:15:13
我不确定这是不是你想要的,但是..
cleaned = stringwithslashes.decode('string_escape')发布于 2008-08-17 12:55:25
听起来你想要的东西可以通过正则表达式相当有效地处理:
import re
def stripslashes(s):
r = re.sub(r"\\(n|r)", "\n", s)
r = re.sub(r"\\", "", r)
return r
cleaned = stripslashes(stringwithslashes)发布于 2014-02-19 03:02:19
使用decode('string_escape')
cleaned = stringwithslashes.decode('string_escape')使用
string_escape:在Python源代码中生成适合作为字符串文字的字符串
或者像威尔逊的答案那样连接replace()。
cleaned = stringwithslashes.replace("\\","").replace("\\n","\n").replace("\\r","\n")https://stackoverflow.com/questions/13454
复制相似问题