如何在字符串文字中放置实际的反斜杠(不将其用于转义序列)?

发布于 2024-09-12 11:33:17 字数 897 浏览 7 评论 0原文

我有这样的代码:

import os
path = os.getcwd()
final = path +'\xulrunner.exe ' + path + '\application.ini'
print(final)

我想要类似的输出:

C:\Users\me\xulrunner.exe C:\Users\me\application.ini

但相反,我得到一个如下所示的错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

我不希望将反斜杠解释为转义序列,而是解释为文字反斜杠。我该怎么做呢?


请注意,如果字符串应该包含反斜杠 - 更一般地,应该在末尾有奇数个反斜杠 - 那么原始字符串无法使用。请参阅如何打印单个反斜杠?。如果您想避免需要转义序列,请参阅如何在 Python 中编写字符串文字而无需转义它们?。< /子>

I have this code:

import os
path = os.getcwd()
final = path +'\xulrunner.exe ' + path + '\application.ini'
print(final)

I want output like:

C:\Users\me\xulrunner.exe C:\Users\me\application.ini

But instead I get an error that looks like:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

I don't want the backslashes to be interpreted as escape sequences, but as literal backslashes. How can I do it?


Note that if the string should only contain a backslash - more generally, should have an odd number of backslashes at the end - then raw strings cannot be used. See How can I print a single backslash?. If you want to avoid the need for escape sequences, see How to write string literals in Python without having to escape them?.

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

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

发布评论

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

评论(4

素年丶 2024-09-19 11:33:17

要直接回答您的问题,请将 r 放在字符串前面。

final= path + r'\xulrunner.exe ' + path + r'\application.ini'

有关Python 网站的更多信息

字符串和字节文字都可以选择以字母“r”或“R”为前缀;此类字符串称为原始字符串,并将反斜杠视为文字字符

但更好的解决方案是os.path.join:(

final = (os.path.join(path, 'xulrunner.exe') + ' ' +
         os.path.join(path, 'application.ini'))

为了便于阅读,我将其分成两行,但你如果您愿意,可以将整个内容放在一行中。)

我会提到您可以在文件路径中使用正斜杠,Python 会根据需要自动将它们转换为正确的分隔符(Windows 上的反斜杠)。所以

final = path + '/xulrunner.exe ' + path + '/application.ini'

应该有效。但最好还是使用 os.path.join ,因为这样可以清楚地表明您要做什么。

To answer your question directly, put r in front of the string.

final= path + r'\xulrunner.exe ' + path + r'\application.ini'

More on Python's site here

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters

But a better solution would be os.path.join:

final = (os.path.join(path, 'xulrunner.exe') + ' ' +
         os.path.join(path, 'application.ini'))

(I split this across two lines for readability, but you could put the whole thing on one line if you want.)

I will mention that you can use forward slashes in file paths, and Python will automatically convert them to the correct separator (backslash on Windows) as necessary. So

final = path + '/xulrunner.exe ' + path + '/application.ini'

should work. But it's still preferable to use os.path.join because that makes it clear what you're trying to do.

﹉夏雨初晴づ 2024-09-19 11:33:17

你可以逃脱斜杠。使用 \\ 只会得到一个斜杠。

You can escape the slash. Use \\ and you get just one slash.

毅然前行 2024-09-19 11:33:17

您可以使用另一个反斜杠 (\\) 转义反斜杠,但它看起来不会更好。要解决此问题,请在字符串前面放置一个 r 来表示原始字符串< /a>.原始字符串将忽略所有转义序列,将反斜杠视为文字文本。它不能包含结束引号,除非它前面有反斜杠(它将包含在字符串中),并且不能以单个反斜杠(或奇数个反斜杠)结尾

You can escape the backslash with another backslash (\\), but it won’t look nicer. To solve that, put an r in front of the string to signal a raw string. A raw string will ignore all escape sequences, treating backslashes as literal text. It cannot contain the closing quote unless it is preceded by a backslash (which will be included in the string), and it cannot end with a single backslash (or odd number of backslashes).

眼泪也成诗 2024-09-19 11:33:17

另一种简单(并且可以说更易读)的方法是使用字符串原始格式和替换,如下所示:

import os
path = os.getcwd()
final = r"{0}\xulrunner.exe {0}\application.ini".format(path)
print(final)

或使用 os 路径方法(以及用于提高可读性的微函数):

import os

def add_cwd(path):
    return os.path.join( os.getcwd(), path )

xulrunner = add_cwd("xulrunner.exe")
inifile = add_cwd("application.ini")
# in production you would use xulrunner+" "+inifile
# but the purpose of this example is to show a version where you could use any character
# including backslash
final = r"{} {}".format( xulrunner, inifile )
print(final)

Another simple (and arguably more readable) approach is using string raw format and replacements like so:

import os
path = os.getcwd()
final = r"{0}\xulrunner.exe {0}\application.ini".format(path)
print(final)

or using the os path method (and a microfunction for readability):

import os

def add_cwd(path):
    return os.path.join( os.getcwd(), path )

xulrunner = add_cwd("xulrunner.exe")
inifile = add_cwd("application.ini")
# in production you would use xulrunner+" "+inifile
# but the purpose of this example is to show a version where you could use any character
# including backslash
final = r"{} {}".format( xulrunner, inifile )
print(final)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文