简体   繁体   English

了解 Python 的反斜杠转义字符

[英]Understanding Python's backslash escape character

My script creates files to export by concatenating a file path with a file type suffix.我的脚本通过将文件路径与文件类型后缀连接来创建要导出的文件。 For example,例如,

int = 3
print r"C:\Test\\" + str(int) + ".jpg"

This returns C:\\Test\\\\3.jpg这将返回C:\\Test\\\\3.jpg

I thought that because I included the 'r' at the beginning, I wouldn't need a double backslash at the end, because it'd be treated as a raw string.我认为因为我在开头包含了 'r',所以在结尾不需要双反斜杠,因为它会被视为原始字符串。 But the following doesn't work:但以下不起作用:

int = 3
print r"C:\Test\" + str(int) + ".jpg"

...presumably because Python is seeing an escape character before the end quote. ...大概是因为 Python 在结束引号之前看到了一个转义字符。 So what's the point of the 'r' then?那么'r'有什么意义呢? And while my files get saved in the right place when exported, I don't like that the print command gives me two backslashes after Test (eg C:\\Test\\3.jpg) when it just gives me one after C:虽然我的文件在导出时保存在正确的位置,但我不喜欢打印命令在测试后给我两个反斜杠(例如 C:\\Test\\3.jpg),而它只在 C 之后给我一个:

How can I get a single backslash in my file paths please?我怎样才能在我的文件路径中得到一个反斜杠?

Don't try and use string manipulation to build up file paths.不要尝试使用字符串操作来构建文件路径。 Python has the os.path module for that: Python 有os.path模块:

import os.path
i = 3
print os.path.join("C:\Test", str(i) + ".jpg")

This will ensure the path is constructed properly.这将确保正确构建路径。

(Also, don't call an integer int , as it shadows the built-in int() function.) (另外,不要调用整数int ,因为它会影响内置的int()函数。)

Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string;即使在原始字符串中,字符串引号也可以用反斜杠转义,但反斜杠仍保留在字符串中; 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 string 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 string, not as a line continuation.例如, r"\\""是由两个字符组成的有效字符串文字:反斜杠和双引号; r"\\"不是有效的字符串文字(即使原始字符串也不能以奇数个反斜杠结尾)。具体来说,原始字符串不能以单个反斜杠结尾(因为反斜杠会转义后面的引号字符)还要注意,单个反斜杠后跟换行符被解释为这两个字符作为字符串的一部分,而不是作为行的延续.

Source . 来源

So, it won't work like that.所以,它不会那样工作。 If you want to use a raw string, but have a trailing backslash, you need to add that separately afterwards:如果你想使用原始字符串,但有一个反斜杠,你需要在之后单独添加:

>>> r'C:\Test' + '\\'
'C:\\Test\\'

Alternatively, in your case, you can also just use string formatting to insert your values:或者,在您的情况下,您也可以仅使用字符串格式来插入您的值:

>>> r'C:\Test\{0}.jpg'.format(i)
'C:\\Test\\1.jpg'

Or, you use os.path.join to build the path:或者,您使用os.path.join来构建路径:

>>> os.path.join(r'C:\Test', str(i) +'.jpg')
'C:\\Test\\1.jpg'

first, don't call an integer int, as it shadows the built-in int() function.首先,不要调用整数 int,因为它隐藏了内置的 int() 函数。
use os.path is a very clever way :使用 os.path 是一个非常聪明的方法

import os.path
i = 3
print os.path.join("C:\Test", str(i) + ".jpg")
#C:\Test\3.jpg

OR U must use strings to manipulation to build up file paths:或者你必须使用字符串来操作来建立文件路径:

_prefix = 'C:\Test'
i = 3
print _prefix + '\\' + str(i) + '.jpg'
#C:\Test\3.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM