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

2021-11-06 16:07:28 +08:00
 eccentric579

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

2631 次点击
所在节点    Python
8 条回复
Dvel
2021-11-06 16:26:09 +08:00
原生字符串的结尾不能是 “反斜杠” + “引号” 。
可以写成 r'D:\下载\Temp\official' + '\\'
festoney8
2021-11-06 16:26:55 +08:00
leoleoasd
2021-11-06 16:41:40 +08:00
根据 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.
leoleoasd
2021-11-06 16:41:49 +08:00
Trim21
2021-11-06 16:43:36 +08:00
只有最后一个反斜杠会失效
eccentric579
2021-11-06 16:48:20 +08:00
谢谢楼上的各位
2i2Re2PLMaDnghL
2021-11-06 23:29:30 +08:00
这是为了让 syntax 简单,把 raw 放到 semantic 层来实现。
O5oz6z3
2021-11-08 14:12:38 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/813501

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX