如何在Python中注释包含字符串中反斜杠的代码块?

huangapple go评论83阅读模式
英文:

How to comment code blocks in Python that contain backslashes in strings?

问题

在Python字符串中,反斜杠表示控制命令的开始,例如,text\n在"text"后添加一个新行。如果不希望字符串中的反斜杠被解释为命令,必须在字符串前面加上r。通常,如果字符串包含Windows目录,如folder=r'C:\u',这就是情况。

然而,如果我想要用"""来注释多行代码,我会遇到错误:

"""
folder=r'C:\u'
"""
(unicode error) 'unicodeescape' codec can't decode bytes in position 13-14: truncated \uXXXX escape

如果逐行使用#来注释,则不会出错,但这种方法不适合注释整个代码块:

#folder=r'C:\u'

如何注释包含反斜杠字符串的整个代码块?

Python 3.10.9,IPython 8.7.0,Spyder 5.4.2,Windows 10

编辑

我不是在寻求类似"不要使用Windows,使用Linux!"的答案或评论。

英文:

A backslash in a Python string indicates the beginning of a control command, for e.g. text\n adds a new line after 'text'. If backslashes in strings shall not be interpreted as commands a r must precede the string. That is usually the case if the string contains a Windows directory, i.e. folder=r'C:\u'.
However, if I want to comment several lines by """ I get an error

"""
folder=r'C:\u'
"""
(unicode error) 'unicodeescape' codec can't decode bytes in position 13-14: truncated \uXXXX escape

If I comment line by line by # then there is no error, however this method is not comfortable for commenting a whole block:

#folder=r'C:\u'

How to comment a whole a block of code that contains strings with backslashes?

Python 3.10.9, IPython 8.7.0, Spyder 5.4.2, Windows 10

Edit

I am not looking for answers or comments in the sense "Don't use Windows, use Linux!"

答案1

得分: 2

你可以在三引号文档字符串前添加r前缀,将其变成原始字符串字面值:

r"""
folder=r'C:\u'
foo='bar'
"""

摘自PEP-257

如果在文档字符串中使用反斜杠符号,请使用r"""raw triple double quotes"""

英文:

You can add the r prefix to a triple-quoted docstring to make it a raw string literal:

r"""
folder=r'C:\u'
foo='bar'
"""

Excerpt from PEP-257:

> Use r"""raw triple double quotes""" if you use any backslashes in your
> docstrings.

答案2

得分: 0

如果你设置一个\,它会被格式化,比如\n表示新行,唯一防止这种情况发生的方法是使用\\,这样Python会明白你只想使用\\而不是格式化。

最后你会得到这样的结果:

"""
folder=r'C:\\u'
"""

否则,你可以像Linux一样写路径,像这样:

"""
folder=r'C:/u'
"""

这不会导致任何格式问题。

英文:

If you set a \ is gonna be formatted, like \n for new line the only way to prevent that is to use \\, with that python gonna understand you just want to use \\ and no a format.

At the end you get that:

"""
folder=r'C:\\u'
"""

Else you can write the path in the same as linux like that:

"""
folder=r'C:/u'
"""

Which don't cause any trouble with format.

答案3

得分: 0

由于您的目标是对一块代码进行注释,正确的方法是在每行前加上#

当你这样做时,解析器会忽略该行的其余部分。您不需要添加r,也不需要担心\

手动进行此操作可能会很繁琐,但任何合适的编辑器都允许您选择一个代码块,并使用单个按键为每行添加注释。(在Spyder中,尝试使用Ctrl-1。其他编辑器通常会使用Ctrl-/或类似的按键组合。)通常,相同的按键会取消选定块上的注释。

三引号的方法实际上不是多行注释,而是多行字符串,解释器正在解析它。将其用作注释不是真正'Pythonic'的做法。如果例如,您要尝试注释的代码块包括三引号括起的字符串,则可能会遇到其他问题。

英文:

Since your goal is to comment a block of code, the correct method is to precede each line with #.

When you do this, the parser ignores the rest of the line. You won't need to add r and you don't need to worry about \.

This would be tedious as a manual process, but any decent editor will allow you to select a block and comment every line with a single keystroke. (In Spyder, try Ctrl-1. Other editors will typically use Ctrl-/ or a variation.) Usually, the same key will uncomment a selected block.

The triple-quote method is not really a multi-line comment, but a multi-line string, which the interpreter is parsing. Using it as a comment is not really 'Pythonic'. You might run into other problems if, for example, the code block you are trying to comment includes triple-quoted strings.

huangapple
  • 本文由 发表于 2023年6月12日 00:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定