【发布时间】:2012-08-26 03:50:44
【问题描述】:
下面代码的输出:
rpl = 'This is a nicely escaped newline \\n'
my_string = 'I hope this apple is replaced with a nicely escaped string'
reg = re.compile('apple')
reg.sub( rpl, my_string )
..是:
'I hope this This is a nicely escaped newline \n is replaced with a nicely escaped string'
..so 打印时:
我希望这是一个很好的转义换行符
被一个很好的转义字符串替换
所以当 python 替换另一个字符串中的 'apple' 时,它是在对字符串进行转义吗?现在我刚刚完成了
reg.sub( rpl.replace('\\','\\\\'), my_string )
这样安全吗?有没有办法阻止 Python 这样做?
【问题讨论】:
-
当你说'下面代码的输出......是'时,这是否意味着你正在使用
print来确定它?还是 REPL? -
@BrianCain,抱歉含糊其辞。这就是字符串的样子。