首页 文章

Python多行字符串的正确缩进

提问于
浏览
395

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

def method():
        string = """line one
line two
line three"""

要么

def method():
        string = """line one
        line two
        line three"""

或者是其他东西?

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

13 回答

  • 1

    你可能想要排队 """

    def foo():
        string = """line one
                 line two
                 line three"""
    

    由于换行符和空格都包含在字符串本身中,因此您必须对其进行后处理 . 如果您不想这样做并且您有大量文本,则可能需要将其单独存储在文本文件中 . 如果文本文件不适合您的应用程序并且您不想进行后期处理,我可能会继续使用

    def foo():
        string = ("this is an "
                  "implicitly joined "
                  "string")
    

    如果要对多行字符串进行后处理以修剪掉不需要的部分,则应考虑使用textwrap模块或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)
    
  • 22

    textwrap.dedent函数允许以 correct indentation in the source 开头,然后在使用前从文本中删除它 .

    正如其他一些人所指出的那样,这种权衡是对文字的额外函数调用;在决定将这些文字放在代码中的位置时,请考虑这一点 .

    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 的返回值是字符串每行上带有 all common leading whitespace indentation removed 的输入字符串,这意味着除了进一步缩进的第三行之外,上面的 log_message 值将向左刷新 .

  • -1

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

    def foo():
        string = ("line one\n"          # Add \n in the string
                  "line two"  "\n"      # Add "\n" after the string
                  "line three\n")
    

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

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

  • 0

    还有一些选择 . 在启用了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()
    
  • 17

    像这样使用inspect.cleandoc

    def method():
        string = inspect.cleandoc("""
            line one
            line two
            line three""")
    

    相对缩进将按预期保持 .

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

  • 3

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

    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
    
  • 414

    我更喜欢

    def method():
            string = \
    """\
    line one
    line two
    line three\
    """
    

    要么

    def method():
            string = """\
    line one
    line two
    line three\
    """
    
  • 17

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

    def foo():
        return "{}\n"\
               "freq: {}\n"\
               "temp: {}\n".format( time, freq, temp )
    
  • 5

    我来到这里寻找一个简单的1-liner到 remove/correct the identation level 的docstring用于打印, without making it look untidy ,例如在脚本中使它成为"hang outside the function" .

    这是我最终做的事情:

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

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

    要正确显示:

    print string.lstrip()
    
  • 0

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

  • 217

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

    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__
    
  • 1

    我做这个...

    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",
        ])
    

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

相关问题