未知的 python 表达式 filename=r'/path/to/file'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19034822/
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
Unknown python expression filename=r'/path/to/file'
提问by Lisa
I found this potentially very useful python script, but came across these expressions which I've never seen before:
我发现这个可能非常有用的 python script,但遇到了这些我以前从未见过的表达式:
inputfilename = r'/path/to/infile'
outputfilename = r'/path/to/outfile'
I can't find a way to search for it. what does the r'...'
do?
我找不到搜索它的方法。是r'...'
做什么的?
Thanks for your help!
谢谢你的帮助!
采纳答案by Maciej Gol
The r'..'
string modifier causes the '..'
string to be interpreted literally. That means, r'My\Path\Without\Escaping'
will evaluate to 'My\Path\Without\Escaping'
- without causing the backslash to escape characters. The prior is equivalent to 'My\\Path\\Without\\Escaping'
string, but without the raw modifier.
该r'..'
修改导致字符串'..'
字符串被解释字面。这意味着,r'My\Path\Without\Escaping'
将评估为'My\Path\Without\Escaping'
- 不会导致反斜杠转义字符。先验等效于'My\\Path\\Without\\Escaping'
字符串,但没有原始修饰符。
Note: The string cannotend with an odd number of backslashes, i.e r'Bad\String\Example\'
is not a correct string.
注意:字符串不能以奇数个反斜杠结尾,即r'Bad\String\Example\'
不是正确的字符串。
回答by falsetru
It's called raw string literal.
它被称为原始字符串文字。
According to Python String literals documentation
:
根据Python String literals documentation
:
The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.
...
Unless an 'r' or 'R' prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C
反斜杠 (\) 字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符。字符串文字可以选择以字母“r”或“R”为前缀;此类字符串称为原始字符串,并使用不同的规则来解释反斜杠转义序列。
...
除非存在 'r' 或 'R' 前缀,字符串中的转义序列将根据与标准 C 使用的规则类似的规则进行解释
回答by Games Brainiac
It just means raw stringin Python. Meaning that whatever is inside the string, is the string. For example, if you wanted to add slashes:
它仅表示Python 中的原始字符串。这意味着字符串中的任何内容都是字符串。例如,如果您想添加斜杠:
string1 = "happy\/cheese"
You would need to add \
in front of the slash since its an escaping character. For example, \n
means a new line.
您需要\
在斜线前面添加,因为它是一个转义字符。例如,\n
表示换行。
If you keep the string raw, it makes sure that whatever is in the string is not interpreted a special way. for example if you wrote string2 = r"\n"
, it would just give you "\n"
as you string, and not a new line.
如果您保持字符串原始状态,它可以确保字符串中的任何内容都不会被以特殊方式解释。例如,如果你写了string2 = r"\n"
,它只会给你"\n"
作为你的字符串,而不是一个新行。
You can learn more about them, from here.
Now, this is done in your case, because file paths have a lot of slashes, and we'd like to avoid having to put in so many backslashes.
现在,这是在您的情况下完成的,因为文件路径有很多斜杠,我们希望避免放入这么多反斜杠。