Python 原始文字字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3517802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python raw literal string
提问by Stan
str = r'c:\path\to\folder\' # my comment
- IDE: Eclipse
- Python2.6
- IDE:日食
- 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.
当字符串中的最后一个字符是反斜杠时,它似乎会转义最后一个单引号并将我的评论视为字符串的一部分。但是原始字符串应该忽略所有转义字符,对吗?可能有什么问题?谢谢。
采纳答案by Alex Martelli
Raw string literals don't treat backslashes as initiating escape sequences exceptwhen the immediately-following character is the quote-character that is delimiting the literal, in which case the backslash doesescape it.
原始字符串文字不会将反斜杠视为启动转义序列,除非紧随其后的字符是分隔文字的引号字符,在这种情况下,反斜杠会对其进行转义。
The design motivation is that raw string literals really exist only for the convenience of entering regular expression patterns – that is all, noother design objective exists for such literals. And RE patterns never need to end with a backslash, but they mightneed to include all kinds of quote characters, whence the rule.
设计动机是原始字符串文字的存在只是为了方便输入正则表达式模式——也就是说,对于此类文字不存在其他设计目标。RE 模式永远不需要以反斜杠结尾,但它们可能需要包含各种引号字符,这就是规则。
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 forwardslashes, which Microsoft's C runtime and all version of Python support as totally equivalent in paths:
许多人确实尝试使用原始字符串文字来使他们能够以他们习惯的方式输入 Windows 路径(使用反斜杠)——但是正如您所注意到的,当您确实需要一个以反斜杠结尾的路径时,这种用法会失效。通常,最简单的解决方案是使用正斜杠,Microsoft 的 C 运行时和所有版本的 Python 都支持在路径中完全等效:
s = 'c:/path/to/folder/'
(side note: don'tshadow 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 usethe builtin name in its real meaning).
(旁注:不要str用你自己的标识符来掩盖内置名称,比如,这是一种可怕的做法,没有任何好处,除非你养成避免这种可怕做法的习惯,否则有一天你会发现自己有一个讨厌的 -调试问题,当您的代码的某些部分践踏内置名称而另一部分需要使用其真正含义的内置名称时)。
回答by Eike
It's IMHO an inconsistency in Python, but it's described in the documentation. Go to the second last paragraph:
恕我直言,这是 Python 中的不一致,但在文档中对此进行了描述。转到倒数第二段:
http://docs.python.org/reference/lexical_analysis.html#string-literals
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)
r"\" 不是有效的字符串文字(即使是原始字符串也不能以奇数个反斜杠结尾)

