A Text Table Writer/Printer for Python

后端 未结 2 1472
渐次进展
渐次进展 2020-12-16 18:18

TL;DR -> Is there a table writing module on PyPi (I\'ve failed to find any) that takes in lists as parameters and makes a table out of those

相关标签:
2条回答
  • 2020-12-16 18:34

    I’m just going to throw a solution from me in which I’ve actually written last week just to test something out. It currently right-aligns everything but it would be simple enough to add some alignment parameters or something.

    def printTable (tbl, borderHorizontal = '-', borderVertical = '|', borderCross = '+'):
        cols = [list(x) for x in zip(*tbl)]
        lengths = [max(map(len, map(str, col))) for col in cols]
        f = borderVertical + borderVertical.join(' {:>%d} ' % l for l in lengths) + borderVertical
        s = borderCross + borderCross.join(borderHorizontal * (l+2) for l in lengths) + borderCross
    
        print(s)
        for row in tbl:
            print(f.format(*row))
            print(s)
    

    Example:

    >>> x = [['Length', 'Time(ms)'], [0, 0], [250, 6], [500, 21], [750, 50], [1000, 87], [1250, 135], [1500, 196], [1750, 269], [2000, 351]]
    >>> printTable(x)
    +--------+----------+
    | Length | Time(ms) |
    +--------+----------+
    |      0 |        0 |
    +--------+----------+
    |    250 |        6 |
    +--------+----------+
    |    500 |       21 |
    +--------+----------+
    |    750 |       50 |
    +--------+----------+
    |   1000 |       87 |
    +--------+----------+
    |   1250 |      135 |
    +--------+----------+
    |   1500 |      196 |
    +--------+----------+
    |   1750 |      269 |
    +--------+----------+
    |   2000 |      351 |
    +--------+----------+
    
    0 讨论(0)
  • 2020-12-16 18:37

    PrettyTable module is what you need:

    PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables.

    >>> import prettytable
    >>> x = prettytable.PrettyTable(["Length", "Time"])
    >>> x.add_row([0, 0.00000])
    >>> x.add_row([250, 0.00600]) 
    >>> x.add_row([500, 0.02100]) 
    >>> x.add_row([750, 0.04999])    
    >>> print x
    +--------+---------+
    | Length |   Time  |
    +--------+---------+
    |   0    |   0.0   |
    |  250   |  0.006  |
    |  500   |  0.021  |
    |  750   | 0.04999 |
    +--------+---------+
    

    Or, texttable:

    texttable is a module to generate a formatted text table, using ASCII characters.

    >>> import texttable
    >>> x = texttable.Texttable()
    >>> x.add_rows([["Length", "Time"], [0, 0.00000], [250, 0.00600], [500, 0.02100], [750, 0.04999]])
    >>> print x.draw()
    +--------+-------+
    | Length | Time  |
    +========+=======+
    | 0      | 0     |
    +--------+-------+
    | 250    | 0.006 |
    +--------+-------+
    | 500    | 0.021 |
    +--------+-------+
    | 750    | 0.050 |
    +--------+-------+
    

    Also see relevant thread: How can I pretty-print ASCII tables with Python?

    0 讨论(0)
提交回复
热议问题