我正在阅读python 2.7的文档,我只是不理解Raw-Unicode-Escape编码。原始文档如下:
For experts, there is also a raw mode just like the one for normal strings. You have to prefix the opening quote with ‘ur’ to have Python use the Raw-Unicode-Escape encoding. It will only apply the above uXXXX conversion if there is an uneven number of backslashes in front of the small ‘u’.
我想知道为什么所需的反斜杠数量不均匀。这只是一个规则还是由于其他原因?
uXXXX
转义是专门在原始字符串中处理的,如您引用的文本所述。 ur'\\'
是一个包含四个反斜杠的字符串,而ur'\u0020\'
是四个反斜杠和一个空格。 如果我必须猜测为什么必须有不均匀数量的反斜杠才能识别u
,我会猜测这是因为非原始字符串解析器也是这样工作的(我没有查看源代码以确定(。
为什么的问题可能归结为python 2的"因为这是定义它的方式"。 Python 3 不再这样做了 - r'\u0020\'
与 '\\\u0020\\'
.