A Text Table Writer/Printer for Python

后端 未结 2 1473
渐次进展
渐次进展 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: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?

提交回复
热议问题