简体   繁体   中英

Pretty-print list without module in an ASCII table

Actually I want to print a list of list like this one for example:

table = [["ESP","South","1000"],["ESP","North","1000"],["ESP","East","1000"],["ESP","West","1000"],["ESP","West","1000"]]

in a ASCII table like that:

+---+-----+----+
|ESP|South|1000|
+---+-----+----+
|ESP|North|1000|
+---+-----+----+
|ESP|East |1000|
+---+-----+----+
|ESP|West |1000|
+---+-----+----+
|ESP|West |1000|
+---+-----+----+

my actual code is:

table = [["ESP","South","1000"],["ESP","North","1000"], 
["ESP","East","1000"],["ESP","West","1000"],["ESP","West","1000"]]

for i in table:
    print("+","-"*7,"+","-"*6,"+","-"*5,"+")
    print("|",i[0]," "*(len(i[0])),"|",
    i[1]," "*(5-len(i[1])),"|",
    i[2]," "*(4-len(i[2])),"|")
print("+","-"*7,"+","-"*6,"+","-"*5,"+")

and the output is:

+ ------- + ------ + ----- +
| ESP     | South  | 1000  |
+ ------- + ------ + ----- +
| ESP     | North  | 1000  |
+ ------- + ------ + ----- +
| ESP     | East   | 1000  |
+ ------- + ------ + ----- +
| ESP     | West   | 1000  |
+ ------- + ------ + ----- +
| ESP     | West   | 1000  |
+ ------- + ------ + ----- +

But the only way I found to do it porperly was with modules like prettytable, pprint, tabulate etc.
And I don't want to use modules at all. What I've done here look good but I want to do it for any list length, because here I've put the exact values I wanted, but if I apply the same code with another list it will not fit. If anybody have an idea of how to do it, it will help me a lot.

You can use this example how to pretty print the table:

table = [
    ["ESP", "South", "1000"],
    ["ESP", "North", "1000"],
    ["ESP", "East", "1000"],
    ["ESP", "West", "1000"],
    ["ESP", "West", "1000"],
]


def pretty_print(table, ch1="-", ch2="|", ch3="+"):
    if len(table) == 0:
        return

    max_lengths = [
        max(column)
        for column in zip(*[[len(cell) for cell in row] for row in table])
    ]

    for row in table:
        print(ch3.join(["", *[ch1 * l for l in max_lengths], ""]))
        print(
            ch2.join(
                [
                    "",
                    *[
                        ("{:<" + str(l) + "}").format(c)
                        for l, c in zip(max_lengths, row)
                    ],
                    "",
                ]
            )
        )
    print(ch3.join(["", *[ch1 * l for l in max_lengths], ""]))


pretty_print(table)

Prints:

+---+-----+----+
|ESP|South|1000|
+---+-----+----+
|ESP|North|1000|
+---+-----+----+
|ESP|East |1000|
+---+-----+----+
|ESP|West |1000|
+---+-----+----+
|ESP|West |1000|
+---+-----+----+

You can customize it:

pretty_print(table, ch1="=")

+===+=====+====+
|ESP|South|1000|
+===+=====+====+
|ESP|North|1000|
+===+=====+====+
|ESP|East |1000|
+===+=====+====+
|ESP|West |1000|
+===+=====+====+
|ESP|West |1000|
+===+=====+====+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM