三重引号转义歧义

1 投票
1 回答
2911 浏览
提问于 2025-04-16 13:15
>>> 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";

在 C# 中。

顺便问一下,我是怎么得到 \x0cile.rtf 而不是 \file.rtf 的呢?

谢谢。

1 个回答

7

Python正在处理\f这个字符。你只需要使用原始字符串就可以了。

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

撰写回答