2011-03-08 60 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