如何在字符串文字中放置实际的反斜杠(不将其用于转义序列)?
我有这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要直接回答您的问题,请将
r
放在字符串前面。有关Python 网站的更多信息
但更好的解决方案是
os.path.join
:(为了便于阅读,我将其分成两行,但你如果您愿意,可以将整个内容放在一行中。)
我会提到您可以在文件路径中使用正斜杠,Python 会根据需要自动将它们转换为正确的分隔符(Windows 上的反斜杠)。所以
应该有效。但最好还是使用 os.path.join ,因为这样可以清楚地表明您要做什么。
To answer your question directly, put
r
in front of the string.More on Python's site here
But a better solution would be
os.path.join
:(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
should work. But it's still preferable to use
os.path.join
because that makes it clear what you're trying to do.你可以逃脱斜杠。使用
\\
只会得到一个斜杠。You can escape the slash. Use
\\
and you get just one slash.您可以使用另一个反斜杠 (
\\
) 转义反斜杠,但它看起来不会更好。要解决此问题,请在字符串前面放置一个r
来表示原始字符串< /a>.原始字符串将忽略所有转义序列,将反斜杠视为文字文本。它不能包含结束引号,除非它前面有反斜杠(它将包含在字符串中),并且不能以单个反斜杠(或奇数个反斜杠)结尾。You can escape the backslash with another backslash (
\\
), but it won’t look nicer. To solve that, put anr
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).另一种简单(并且可以说更易读)的方法是使用字符串原始格式和替换,如下所示:
或使用 os 路径方法(以及用于提高可读性的微函数):
Another simple (and arguably more readable) approach is using string raw format and replacements like so:
or using the os path method (and a microfunction for readability):