python中r开头的字符串代表里面的反斜杠不转义,比如:
>>> s = r"asd\a"
>>> s
'asd\\a'
上面的结果很容易理解,但是反斜杠如果出现在字符串的结尾则不行:
>>> s = r"asd\"
File "<stdin>", line 1
s = r"asd\"
^
SyntaxError: EOL while scanning string literal
为什么会有这样的设计呢?既然r的意思是不转义,为何在末尾的时候又会出现错误呢?
我在 stack overflow 找到了前人問題程答案:
https://stackoverflow.com/que...
"When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. ......."
上面錯誤已刪
已作修改:
所以我想,可能因為在r''中,反斜杠后面的 ' 被當作字符保留了而且是一般字符,因而是失去了功能。
又或者是因為,例如r"abc" , 先生成"abc" 字符串,然後才加上r再處理, 如果r"反斜杠"就出現生成字符串失敗。
Doc. Python 里面建意r"反斜杠""
抱歉,反斜杠不見了,希望你
https://docs.python.org/2.0/r...
Doc.python 有陳述