Python 原始文字字符串

发布于 2024-09-15 12:34:40 字数 208 浏览 4 评论 0原文

str = r'c:\path\to\folder\'   # my comment
  • IDE:Eclipse
  • Python2.6

当字符串中的最后一个字符是反斜杠时,它似乎会转义最后一个单引号并将我的注释视为字符串的一部分。但原始字符串应该忽略所有转义字符,对吧?可能出什么问题了?谢谢。

str = r'c:\path\to\folder\'   # my comment
  • IDE: Eclipse
  • Python2.6

When the last character in the string is a backslash, it seems like it will escape the last single quote and treat my comment as part of the string. But the raw string is supposed to ignore all escape characters, right? What could be wrong? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-09-22 12:34:40

原始字符串文字不会将反斜杠视为启动转义序列除非当紧随其后的字符是分隔文字的引号字符时,在这种情况下反斜杠逃避它。

设计动机是,原始字符串文字实际上只是为了方便输入正则表达式模式而存在 - 也就是说,此类文字不存在其他设计目标。 RE 模式永远不需要以反斜杠结尾,但它们可能需要包含各种引号字符,这就是规则的来源。

许多人确实尝试使用原始字符串文字来使他们能够按照习惯的方式(使用反斜杠)输入 Windows 路径 - 但正如您所注意到的,当您确实需要一个以反斜杠结尾的路径时,这种使用就会失败。通常,最简单的解决方案是使用正斜杠,微软的C运行时和所有版本的Python都支持在路径中完全等效:(

s = 'c:/path/to/folder/'

旁注:不要内置阴影名称,如 str,带有您自己的标识符 - 这是一种可怕的做法,没有任何好处,除非您养成避免这种可怕做法的习惯,有一天您会发现自己陷入了一种令人讨厌的境地-调试问题,当代码的某些部分践踏内置名称而另一部分需要使用真正含义的内置名称时)。

Raw string literals don't treat backslashes as initiating escape sequences except when the immediately-following character is the quote-character that is delimiting the literal, in which case the backslash does escape it.

The design motivation is that raw string literals really exist only for the convenience of entering regular expression patterns – that is all, no other design objective exists for such literals. And RE patterns never need to end with a backslash, but they might need to include all kinds of quote characters, whence the rule.

Many people do try to use raw string literals to enable them to enter Windows paths the way they're used to (with backslashes) – but as you've noticed this use breaks down when you do need a path to end with a backslash. Usually, the simplest solution is to use forward slashes, which Microsoft's C runtime and all version of Python support as totally equivalent in paths:

s = 'c:/path/to/folder/'

(side note: don't shadow builtin names, like str, with your own identifiers – it's a horrible practice, without any upside, and unless you get into the habit of avoiding that horrible practice one day you'll find yourseld with a nasty-to-debug problem, when some part of your code tramples over a builtin name and another part needs to use the builtin name in its real meaning).

余生共白头 2024-09-22 12:34:40

恕我直言,这是 Python 中的不一致,但在文档中对此进行了描述。转到倒数第二段:

http://docs.python.org/reference/ lexical_analysis.html#string-literals

r"\" 不是有效的字符串文字
(即使是原始字符串也不能以
奇数个反斜杠)

It's IMHO an inconsistency in Python, but it's described in the documentation. Go to the second last paragraph:

http://docs.python.org/reference/lexical_analysis.html#string-literals

r"\" is not a valid string literal
(even a raw string cannot end in an
odd number of backslashes)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文