2011-03-08 55 views
1
>>> sample7 = """including 'quote1' "quote2" and 'quote3" """ 
>>> sample7 
'including \'quote1\' "quote2" and \'quote3" ' 
>>> print sample7 
including 'quote1' "quote2" and 'quote3" 

在这里,字符串sample7内部的引号被三重引号正确转义。但是,三重报价逃避歧义

>>> sample4 = """c:\ntext\text\file.rtf""" 
>>> sample4 
'c:\ntext\text\x0cile.rtf' 
>>> print sample4 
c: 
text extile.rtf 

sample4反斜线是没有正确三重报价逃脱。这是为什么?应该做些什么来自动逃避。类似的,

String file = @"c:\ntext\text\file.rtf"; 

in C#。

PS。另外,我怎么得到\x0cile.rtf而不是\file.rtf

谢谢。

回答

7

Python是评估\˚F只需使用原始字符串

r"c:\ntext\text\file.rtf" 
+0

@mirabilos我找不到它不是由原始字符串忽略任何转义序列。唯一的例外是原始字符串不能以奇数个斜线结束。例如。 'print r'test \\\\“'yield test \\\\但'print r'test \\'”'给出错误。 – Jim 2015-06-17 11:10:30

+0

@Jim对,正好。这对于自动转义来说确实是一件有问题的事情。比较shell,它非常容易:前缀''',后缀''',用'''''替换里面的所有''',完成。 – mirabilos 2015-06-17 12:31:17