为什么Python的原始字符串文字不能以单个反斜杠结尾? [英] Why can't Python's raw string literals end with a single backslash?
问题描述
从技术上讲,如中所述,任意数量的反斜杠文档.
>>> r'\'
File "<stdin>", line 1
r'\'
^
SyntaxError: EOL while scanning string literal
>>> r'\\'
'\\\\'
>>> r'\\\'
File "<stdin>", line 1
r'\\\'
^
SyntaxError: EOL while scanning string literal
似乎解析器可以将原始字符串中的反斜杠视为常规字符(这不是原始字符串的全部含义吗?),但是我可能缺少明显的东西.
It seems like the parser could just treat backslashes in raw strings as regular characters (isn't that what raw strings are all about?), but I'm probably missing something obvious.
推荐答案
原因在本部分中以粗体突出显示的部分进行了解释:
The reason is explained in the part of that section which I highlighted in bold:
字符串引号可以使用 反斜杠,但反斜杠仍然存在 在字符串中;例如,
r"\""
是一个 由两个组成的有效字符串文字 字符:一个反斜杠和一个双 引用;r"\"
不是有效的字符串 文字(即使原始字符串也不能结束 以奇数个反斜杠表示). 具体来说,原始字串无法结束 在一个反斜杠中(因为 反斜杠将逃避以下内容 引号字符).另请注意, 单个反斜杠后跟换行符 被解释为这两个字符 作为字符串的一部分,而不是一行 延续.
String quotes can be escaped with a backslash, but the backslash remains in the string; for example,
r"\""
is a valid string literal consisting of two characters: a backslash and a double quote;r"\"
is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
因此原始字符串不是100%原始的,仍然存在一些基本的反斜杠处理.
So raw strings are not 100% raw, there is still some rudimentary backslash-processing.
这篇关于为什么Python的原始字符串文字不能以单个反斜杠结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!