Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';是python中dedent()的函数_Python - Fatal编程技术网

什么';是python中dedent()的函数

什么';是python中dedent()的函数,python,Python,代码如下。我真的很困惑dedent()的函数是什么,当我运行这些代码时,结果几乎是一样的 print(dedent(""" Like a world class boxer you dodge, weave, slip and slide right as the Gothon's blaster cranks a laser past your head. In the middle of your artful dodge your foot slips and you bang your

代码如下。我真的很困惑dedent()的函数是什么,当我运行这些代码时,结果几乎是一样的

print(dedent("""
Like a world class boxer you dodge, weave, slip and
slide right as the Gothon's blaster cranks a laser
past your head. In the middle of your artful dodge
your foot slips and you bang your head on the metal
wall and pass out. You wake up shortly after only to
die as the Gothon stomps on your head and eats you.
"""))

print("""
Like a world class boxer you dodge, weave, slip and
slide right as the Gothon's blaster cranks a laser
past your head. In the middle of your artful dodge
your foot slips and you bang your head on the metal
wall and pass out. You wake up shortly after only to
die as the Gothon stomps on your head and eats you.
""")
基于

考虑以下可能有用的示例:

import textwrap

# We have some sample data
data = [[13,"John","New York"],
        [25,"Jane","Madrid"]]

# We create a print function
def printfunc(lst):

    # This is the string we want to add data to

    how_string_could_look_like = """\
age: {}
name: {}
city: {}
"""

    # But we can write it this way instead and use dedent:
    # Much easier to read right? (Looks neat inside the function)

    string = """\
    age: {}
    name: {}
    city: {}
    """

    for item in lst:
        p = string.format(*item)
        print(p) # print without dedent
        print(textwrap.dedent(p)) # print with dedent

printfunc(data)
印刷品:

请看这里的第1组和第3组(无dedent)与第2组和第4组(有dedent)的比较。这个例子说明了dedent可以做什么

    age: 13
    name: John
    city: New York

age: 13
name: John
city: New York

    age: 25
    name: Jane
    city: Madrid

age: 25
name: Jane
city: Madrid
回顾您的示例:

思考一下:我想要textwrap.dedent是什么(删除每行前的空白)

没有注释的完整示例

data = [[13,"John","New York"],[25,"Jane","Madrid"]]

def printfunc(lst):

    for item in lst:

        string = """\
        age: {}
        name: {}
        city: {}
        """.format(*item)

        print(textwrap.dedent(string))

printfunc(data)

当然,如果在非缩进文本上运行
dedent
。。。你读过它的文档了吗?请看这里:你的
dedent
,结果如何“几乎”相同?可能是重复的