How to remove extra indentation of Python triple quoted multi-line strings?

后端 未结 6 1009
野的像风
野的像风 2020-12-23 14:46

I have a python editor where the user is entering a script or code, which is then put into a main method behind the scenes, while also having every line indented. The proble

6条回答
  •  生来不讨喜
    2020-12-23 15:15

    From what I see, a better answer here might be inspect.cleandoc, which does much of what textwrap.dedent does but also fixes the problems that textwrap.dedent has with the leading line.

    The below example shows the differences:

    >>> import textwrap
    >>> import inspect
    >>> x = """foo bar
        baz
        foobar
        foobaz
        """
    >>> inspect.cleandoc(x)
    'foo bar\nbaz\nfoobar\nfoobaz'
    >>> textwrap.dedent(x)
    'foo bar\n    baz\n    foobar\n    foobaz\n'
    >>> y = """
    ...     foo
    ...     bar
    ... """
    >>> textwrap.dedent(y)
    '\nfoo\nbar\n'
    >>> inspect.cleandoc(y)
    'foo\nbar'
    >>> z = """\tfoo
    bar\tbaz
    """
    >>> textwrap.dedent(z)
    '\tfoo\nbar\tbaz\n'
    >>> inspect.cleandoc(z)
    'foo\nbar     baz'
    

    Note that inspect.cleandoc also expands internal tabs to spaces. This may be inappropriate for one's use case, but works fine for me.

提交回复
热议问题