10

为什么 raw 的 r 禁止转义会失效呢

 2 years ago
source link: https://www.v2ex.com/t/813501
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

为什么 raw 的 r 禁止转义会失效呢

  eccentric579 · 7 小时 33 分钟前 · 546 次点击

aim = {
    "official" : r"D:\下载\Temp\official\",
    "others" : r"D:\下载\Temp\others\"
}



7 条回复    2021-11-06 23:29:30 +08:00

Dvel   7 小时 14 分钟前

原生字符串的结尾不能是 “反斜杠” + “引号” 。可以写成 r'D:\下载\Temp\official' + '\\'

leoleoasd   6 小时 59 分钟前

根据 python 文档:Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; 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 literal 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 literal, not as a line continuation.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK