python输入多行字符串_Python多行字符串的正确缩进

Python多行字符串的正确缩进

函数中Python多行字符串的正确缩进是什么?

def method():

string = """line one

line two

line three"""

要么

def method():

string = """line one

line two

line three"""

或者是其他东西?

在第一个示例中将字符串悬挂在函数外部看起来有点奇怪。

ensnare asked 2019-01-03T21:51:31Z

13个解决方案

413 votes

您可能想要与textwrap对齐

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)

Mike Graham answered 2019-01-03T21:51:52Z

215 votes

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)

日志消息文字中的尾随textwrap.dedent是为了确保换行符不在文字中; 这样,文字不会以空行开头,而是从下一个完整行开始。

textwrap.dedent的返回值是输入字符串,在字符串的每一行上删除了所有公共前导空格缩进,这意味着除了进一步缩进的第三行之外,上面的log_message值将向左刷新。

bignose answered 2019-01-03T21:52:34Z

21 votes

其他答案中似乎缺少的一个选项(仅在naxa的评论中深入提及)如下:

def foo():

string = ("line one\n" # Add \n in the string

"line two" "\n" # Add "\n" after the string

"line three\n")

这将允许正确对齐,隐含地连接线,并仍然保持线移位,对我来说,这是我想要使用多线字符串的原因之一。

它不需要任何后处理,但您需要在任何您希望该行结束的给定位置手动添加\n。 内联或后面的单独字符串。 后者更容易复制粘贴。

holroy answered 2019-01-03T21:53:09Z

17 votes

还有一些选择。 在启用了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函数使得代码在字符串定义时更具可读性,但需要稍后处理以获得可用格式的字符串。 在文档字符串中,显然您应该使用正确的缩进,因为docstring的大多数用法都将执行所需的处理。

当我在我的代码中需要一个非长字符串时,我发现以下公认的丑陋代码,我让长字符串从封闭的缩进中删除。 绝对失败的是“美丽胜过丑陋。”但是有人可能会认为它比狡猾的选择更简单,更明确。

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()

Joop answered 2019-01-03T21:54:04Z

16 votes

像这样使用string:

def method():

string = inspect.cleandoc("""

line one

line two

line three""")

相对缩进将按预期保持。

注意:最好在其相关上下文中缩进代码的逻辑块以阐明结构。 例如。 属于变量string的多行字符串。

wihlke answered 2019-01-03T21:54:39Z

5 votes

如果你想要一个快速简单的解决方案并且不用输入换行符,你可以选择一个列表,例如:

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

steabert answered 2019-01-03T21:55:01Z

4 votes

我更喜欢

def method():

string = \

"""\

line one

line two

line three\

"""

要么

def method():

string = """\

line one

line two

line three\

"""

lk_vc answered 2019-01-03T21:55:19Z

3 votes

我的两分钱,逃避行尾以获得缩进:

def foo():

return "{}\n"\

"freq: {}\n"\

"temp: {}\n".format( time, freq, temp )

Simon answered 2019-01-03T21:55:41Z

1 votes

我来到这里寻找一个简单的1-liner来删除/更正用于打印的文档字符串的标识级别,而不会让它看起来不整洁,例如通过使其在脚本中“挂在函数外”。

这是我最终做的事情:

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')

James Gowdy answered 2019-01-03T21:56:23Z

1 votes

第一个选择是好的 - 包括缩进。它是python风格 - 提供代码的可读性。

要正确显示:

print string.lstrip()

Bog Dia answered 2019-01-03T21:56:50Z

0 votes

这取决于您希望文本显示的方式。 如果你想让它全部左对齐,那么要么像第一个片段那样格式化它,要么迭代左边的行 - 修剪所有空格。

Ignacio Vazquez-Abrams answered 2019-01-03T21:57:12Z

0 votes

对于字符串,您可以在处理字符串之后。 对于文档字符串,您需要在处理函数之后。 这是两个仍然可读的解决方案。

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__

geckos answered 2019-01-03T21:57:34Z

-1 votes

我这样做......

part = "".join([

"\x00\x00\x00\x00\x0C\x00\x00\x00\xFF\x00\x00\x00\x00\x00\x00\x00"

"\x00\x00\x00\x00\x0C\x00\x00\x00\x00\xFF\x00\x00\x00\x00\x00\x00",

"\x00\x00\x00\x00\x0C\x00\x00\x00\x00\x00\xFF\x00\x00\x00\x00\x00",

"\x00\x00\x00\x00\x0C\x00\x00\x00\x00\x00\x00\xFF\x00\x00\x00\x00",

])

不是我知道的最有效,但它很整洁,不需要“后处理”,并且对于大多数用例而言性能足够高。

technicalbloke answered 2019-01-03T21:58:03Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值