问题:正确缩进Python多行字符串
函数中Python多行字符串的正确缩进是什么?
def method():
string = """line one
line two
line three"""
要么
def method():
string = """line one
line two
line three"""
或者是其他东西?
在第一个示例中,将字符串挂在函数外部看起来有些奇怪。
What is the proper indentation for Python multiline strings within a function?
def method():
string = """line one
line two
line three"""
or
def method():
string = """line one
line two
line three"""
or something else?
It looks kind of weird to have the string hanging outside the function in the first example.
回答 0
您可能想与 """
def foo():
string = """line one
line two
line three"""
由于换行符和空格包含在字符串本身中,因此您必须对其进行后处理。如果您不想这样做,并且文本很多,则可能需要将其分别存储在文本文件中。如果文本文件不能很好地适合您的应用程序,并且您不想进行后处理,那么我可能会选择
def foo():
string = ("this is an "
"implicitly joined "
"string")
如果要对多行字符串进行后处理以修剪掉不需要的部分,则应考虑PEP 257中textwrap
介绍的对文档字符串进行后处理的模块或技术:
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
You probably want to line up with the """
def foo():
string = """line one
line two
line three"""
Since the newlines and spaces are included in the string itself, you will have to postprocess it. If you don’t want to do that and you have a whole lot of text, you might want to store it separately in a text file. If a text file does not work well for your application and you don’t want to postprocess, I’d probably go with
def foo():
string = ("this is an "
"implicitly joined "
"string")
If you want to postprocess a multiline string to trim out the parts you don’t need, you should consider the textwrap
module or the technique for postprocessing docstrings presented in PEP 257:
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
回答 1
该textwrap.dedent
功能允许在源代码中以正确的缩进开始,然后在使用前从文本中删除它。
正如其他一些人所指出的那样,这是对文字的一个额外的函数调用。在决定将这些文字放在代码中的位置时,请考虑到这一点。
import textwrap
def frobnicate(param):
""" Frobnicate the scrognate param.
The Weebly-Ruckford algorithm is employed to frobnicate
the scrognate to within an inch of its life.
"""
prepare_the_comfy_chair(param)
log_message = textwrap.dedent("""\
Prepare to frobnicate:
Here it comes...
Any moment now.
And: Frobnicate!""")
weebly(param, log_message)
ruckford(param)
\
日志消息文字中的结尾是为了确保换行符不在文字中;这样,文字不以空白行开头,而是以下一个完整行开头。
from的返回值textwrap.dedent
是输入字符串,在字符串的每一行上都删除了所有常见的前导空格。因此,上面的log_message
值将是:
Prepare to frobnicate:
Here it comes...
Any moment now.
And: Frobnicate!
The textwrap.dedent
function allows one to start with correct indentation in the source, and then strip it from the text before use.
The trade-off, as noted by some others, is that this is an extra function call on the literal; take this into account when deciding where to place these literals in your code.
import textwrap
def frobnicate(param):
""" Frobnicate the scrognate param.
The Weebly-Ruckford algorithm is employed to frobnicate
the scrognate to within an inch of its life.
"""
prepare_the_comfy_chair(param)
log_message = textwrap.dedent("""\
Prepare to frobnicate:
Here it comes...
Any moment now.
And: Frobnicate!""")
weebly(param, log_message)
ruckford(param)
The trailing \
in the log message literal is to ensure that line break isn’t in the literal; that way, the literal doesn’t start with a blank line, and instead starts with the next full line.
The return value from textwrap.dedent
is the input string with all common leading whitespace indentation removed on each line of the string. So the above log_message
value will be:
Prepare to frobnicate:
Here it comes...
Any moment now.
And: Frobnicate!
回答 2
inspect.cleandoc
像这样使用:
def method():
string = inspect.cleandoc("""
line one
line two
line three""")
相对缩进将保持预期。正如评论下面,如果你想保持使用前空行,textwrap.dedent
。但是,这样也可以保持第一行。
注意:优良作法是在代码的相关上下文下缩进逻辑代码块以阐明结构。例如,属于变量的多行字符串string
。
Use inspect.cleandoc
like so:
def method():
string = inspect.cleandoc("""
line one
line two
line three""")
Relative indentation will be maintained as expected. As commented below, if you want to keep preceding empty lines, use textwrap.dedent
. However that also keeps the first line break.
Note: It’s good practice to indent logical blocks of code under its related context to clarify the structure. E.g. the multi-line string belonging to the variable string
.
回答 3
以下似乎是其他答案(仅在naxa的评论的最下方提到)中缺少的一个选项:
def foo():
string = ("line one\n" # Add \n in the string
"line two" "\n" # Add "\n" after the string
"line three\n")
这将允许正确对齐,隐式连接行并仍保持行移位,这对我来说还是我仍然要使用多行字符串的原因之一。
它不需要任何后处理,但是您需要\n
在要结束行的任何给定位置手动添加。内联或后接一个单独的字符串。后者更容易复制粘贴。
One option which seems to missing from the other answers (only mentioned deep down in a comment by naxa) is the following:
def foo():
string = ("line one\n" # Add \n in the string
"line two" "\n" # Add "\n" after the string
"line three\n")
This will allow proper aligning, join the lines implicitly, and still keep the line shift which, for me, is one of the reasons why I would like to use multiline strings anyway.
It doesn’t require any postprocessing, but you need to manually add the \n
at any given place that you want the line to end. Either inline or as a separate string after. The latter is easier to copy-paste in.
回答 4
一些更多的选择。在启用pylab的Ipython中,dedent已经在命名空间中。我检查了,它来自matplotlib。或者可以将其导入:
from matplotlib.cbook import dedent
在文档中它指出它比等效的textwrap更快,在我的ipython测试中,它的确比我的快速测试平均快3倍。它还具有丢弃任何前导空白行的好处,这使您可以灵活地构造字符串:
"""
line 1 of string
line 2 of string
"""
"""\
line 1 of string
line 2 of string
"""
"""line 1 of string
line 2 of string
"""
在这三个示例上使用matplotlib dedent将产生相同的明智结果。textwrap dedent函数在第一个示例中将有一个前导空白行。
明显的缺点是textwrap在标准库中,而matplotlib是外部模块。
这里有一些折衷… dedent函数使您的代码在定义字符串的地方更具可读性,但是需要稍后进行处理才能以可用格式获取字符串。在文档字符串中,很明显应该使用正确的缩进,因为文档字符串的大多数用法都会进行所需的处理。
当我的代码中需要一个非长字符串时,我发现以下公认的丑陋代码,在其中让长字符串脱离了封闭的缩进。肯定在“美丽比丑陋更好”上失败了,但是有人会说它比坚决的选择更简单,更明确。
def example():
long_string = '''\
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip.\
'''
return long_string
print example()
Some more options. In Ipython with pylab enabled, dedent is already in the namespace. I checked and it is from matplotlib. Or it can be imported with:
from matplotlib.cbook import dedent
In documentation it states that it is faster than the textwrap equivalent one and in my tests in ipython it is indeed 3 times faster on average with my quick tests. It also has the benefit that it discards any leading blank lines this allows you to be flexible in how you construct the string:
"""
line 1 of string
line 2 of string
"""
"""\
line 1 of string
line 2 of string
"""
"""line 1 of string
line 2 of string
"""
Using the matplotlib dedent on these three examples will give the same sensible result. The textwrap dedent function will have a leading blank line with 1st example.
Obvious disadvantage is that textwrap is in standard library while matplotlib is external module.
Some tradeoffs here… the dedent functions make your code more readable where the strings get defined, but require processing later to get the string in usable format. In docstrings it is obvious that you should use correct indentation as most uses of the docstring will do the required processing.
When I need a non long string in my code I find the following admittedly ugly code where I let the long string drop out of the enclosing indentation. Definitely fails on “Beautiful is better than ugly.”, but one could argue that it is simpler and more explicit than the dedent alternative.
def example():
long_string = '''\
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip.\
'''
return long_string
print example()
回答 5
如果您想要一个快速简便的解决方案并避免输入换行符,则可以选择一个列表,例如:
def func(*args, **kwargs):
string = '\n'.join([
'first line of very long string and',
'second line of the same long thing and',
'third line of ...',
'and so on...',
])
print(string)
return
If you want a quick&easy solution and save yourself from typing newlines, you could opt for a list instead, e.g.:
def func(*args, **kwargs):
string = '\n'.join([
'first line of very long string and',
'second line of the same long thing and',
'third line of ...',
'and so on...',
])
print(string)
return
回答 6
我更喜欢
def method():
string = \
"""\
line one
line two
line three\
"""
要么
def method():
string = """\
line one
line two
line three\
"""
I prefer
def method():
string = \
"""\
line one
line two
line three\
"""
or
def method():
string = """\
line one
line two
line three\
"""
回答 7
我的两分钱,逃离行尾以获取缩进:
def foo():
return "{}\n"\
"freq: {}\n"\
"temp: {}\n".format( time, freq, temp )
My two cents, escape the end of line to get the indents:
def foo():
return "{}\n"\
"freq: {}\n"\
"temp: {}\n".format( time, freq, temp )
回答 8
我来这里是为了寻找一种简单的1-衬板,以去除/校正打印时文档字符串的标识级别,而又不会使其看起来不整洁,例如,通过使其在脚本内“挂在函数外部”。
我最终要做的是:
import string
def myfunction():
"""
line 1 of docstring
line 2 of docstring
line 3 of docstring"""
print str(string.replace(myfunction.__doc__,'\n\t','\n'))[1:]
显然,如果要缩进空格(例如4)而不是Tab键,请改用如下代码:
print str(string.replace(myfunction.__doc__,'\n ','\n'))[1:]
而且,如果您希望文档字符串看起来像这样,则无需删除第一个字符:
"""line 1 of docstring
line 2 of docstring
line 3 of docstring"""
print string.replace(myfunction.__doc__,'\n\t','\n')
I came here looking for a simple 1-liner to remove/correct the identation level of the docstring for printing, without making it look untidy, for example by making it “hang outside the function” within the script.
Here’s what I ended up doing:
import string
def myfunction():
"""
line 1 of docstring
line 2 of docstring
line 3 of docstring"""
print str(string.replace(myfunction.__doc__,'\n\t','\n'))[1:]
Obviously, if you’re indenting with spaces (e.g. 4) rather than the tab key use something like this instead:
print str(string.replace(myfunction.__doc__,'\n ','\n'))[1:]
And you don’t need to remove the first character if you like your docstrings to look like this instead:
"""line 1 of docstring
line 2 of docstring
line 3 of docstring"""
print string.replace(myfunction.__doc__,'\n\t','\n')
回答 9
第一种选择是好的-包括缩进。它是python样式-提供代码的可读性。
要正确显示它:
print string.lstrip()
The first option is the good one – with indentation included. It is in python style – provides readability for the code.
To display it properly:
print string.lstrip()
回答 10
这取决于您希望文本如何显示。如果您希望所有内容都左对齐,则可以按照第一个代码段的格式对其进行格式化,也可以遍历所有空间的左行进行迭代。
It depends on how you want the text to display. If you want it all to be left-aligned then either format it as in the first snippet or iterate through the lines left-trimming all the space.
回答 11
对于字符串,您可以在处理字符串之后。对于文档字符串,您需要对函数进行后处理。这是一个仍然可读的解决方案。
class Lstrip(object):
def __rsub__(self, other):
import re
return re.sub('^\n', '', re.sub('\n$', '', re.sub('\n\s+', '\n', other)))
msg = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
''' - Lstrip()
print msg
def lstrip_docstring(func):
func.__doc__ = func.__doc__ - Lstrip()
return func
@lstrip_docstring
def foo():
'''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
'''
pass
print foo.__doc__
For strings you can just after process the string. For docstrings you need to after process the function instead. Here is a solution for both that is still readable.
class Lstrip(object):
def __rsub__(self, other):
import re
return re.sub('^\n', '', re.sub('\n$', '', re.sub('\n\s+', '\n', other)))
msg = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
''' - Lstrip()
print msg
def lstrip_docstring(func):
func.__doc__ = func.__doc__ - Lstrip()
return func
@lstrip_docstring
def foo():
'''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
'''
pass
print foo.__doc__
回答 12
我遇到类似的问题,使用多行代码使代码变得难以理解,我想到了类似
print("""aaaa
""" """bbb
""")
是的,一开始看起来可能很糟糕,但是嵌入式语法非常复杂,并且在末尾添加一些内容(例如’\ n“’)不是解决方案
I’m having a similar issue, code got really unreadable using multilines, I came out with something like
print("""aaaa
""" """bbb
""")
yes, at beginning could look terrible but the embedded syntax was quite complex and adding something at the end (like ‘\n”‘) was not a solution
回答 13
您可以使用此函数trim_indent。
import re
def trim_indent(s: str):
s = re.sub(r'^\n+', '', s)
s = re.sub(r'\n+$', '', s)
spaces = re.findall(r'^ +', s, flags=re.MULTILINE)
if len(spaces) > 0 and len(re.findall(r'^[^\s]', s, flags=re.MULTILINE)) == 0:
s = re.sub(r'^%s' % (min(spaces)), '', s, flags=re.MULTILINE)
return s
print(trim_indent("""
line one
line two
line three
line two
line one
"""))
结果:
"""
line one
line two
line three
line two
line one
"""
You can use this function trim_indent.
import re
def trim_indent(s: str):
s = re.sub(r'^\n+', '', s)
s = re.sub(r'\n+$', '', s)
spaces = re.findall(r'^ +', s, flags=re.MULTILINE)
if len(spaces) > 0 and len(re.findall(r'^[^\s]', s, flags=re.MULTILINE)) == 0:
s = re.sub(r'^%s' % (min(spaces)), '', s, flags=re.MULTILINE)
return s
print(trim_indent("""
line one
line two
line three
line two
line one
"""))
Result:
"""
line one
line two
line three
line two
line one
"""
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。