Search code examples
pythonarraysfor-loopmultidimensional-arraypython-beautifultable

Printing out multidimensional arrays using python Beautiful Table module


I am trying to print out a multidimensional array in tabular format using Beautiful Table module in Python2.7.

I have tried the following code:

from beautifultable import BeautifulTable
tableA = BeautifulTable()
tableA.column_headers = ["possible Inflow", "prob", "possibleinlow * prob", "d12p"]
data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0], [  8.,  12.,   3.,   8.], [ 1458.,  3136.,   784.,  3364.]]
tableA.column_headers = ["inputs", "possible Inflow", "prob", "possibleinlow * prob", "d12p"]
for i in range(len(data)):
    tableA.append_row([item[i] for item in data])
print(tableA)

But it prints out the following error:

Traceback (most recent call last):File "test.py", line 16, in <module>

"raise ValueError("'Expected iterable of length {}, got {}".format(self._column_count, len(row)))

ValueError: 'Expected iterable of length 4, got 5"


Solution

  • @Imm, assuming your first

    tableA.column_headers = 
    

    is the one you want (4 items) and you have 5 arrays of 4 items that you are adding to the table as rows then you may want the simplier program:

    from beautifultable import BeautifulTable
    tableA = BeautifulTable()
    tableA.column_headers = ["possible Inflow", "prob", "possibleinlow * prob", "d12p"]
    data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0],\
     [8., 12., 3., 8.], [ 1458., 3136., 784., 3364.]]
    for i in range(len(data)):
         tableA.append_row(data[i])
    print(tableA)
    

    ... which produces:

    +-----------------+------+----------------------------------------------+------+
    | possible Inflow | prob |             possibleinlow * prob             | d12p |
    +-----------------+------+----------------------------------------------+------+
    |        a        |  1   |                      2                       |  2   |
    +-----------------+------+----------------------------------------------+------+
    |        4        |  3   |                      3                       |  2   |
    +-----------------+------+----------------------------------------------+------+
    |        2        |  4   |                      1                       |  4   |
    +-----------------+------+----------------------------------------------+------+
    |        8        |  12  |                      3                       |  8   |
    +-----------------+------+----------------------------------------------+------+
    |      1458       | 3136 |                     784                      | 3364 |
    +-----------------+------+----------------------------------------------+------+
    

    If you instead want to use the second

    tabelA.column_headers = 
    

    line and your data is groups of column information then just simply use one of the column methods of BeautifulTable.

    from beautifultable import BeautifulTable
    tableA = BeautifulTable()
    columnHeaders = ["inputs", "possible Inflow", "prob", "possibleinlow * prob", "d12p"]
    data = [['a', 1, 2, 2], [4.0, 3.0, 3.0, 2.0], [2.0, 4.0, 1.0, 4.0],\
     [8., 12., 3., 8.], [1458., 3136., 784., 3364.]]
    for i in range(len(columnHeaders)):
        tableA.insert_column(i, columnHeaders[i], data[i])
    print(tableA)
    

    ... which produces:

    +--------+------------------------+------+------------------------------+------+
    | inputs |    possible Inflow     | prob |     possibleinlow * prob     | d12p |
    +--------+------------------------+------+------------------------------+------+
    |   a    |           4            |  2   |              8               | 1458 |
    +--------+------------------------+------+------------------------------+------+
    |   1    |           3            |  4   |              12              | 3136 |
    +--------+------------------------+------+------------------------------+------+
    |   2    |           3            |  1   |              3               | 784  |
    +--------+------------------------+------+------------------------------+------+
    |   2    |           2            |  4   |              8               | 3364 |
    +--------+------------------------+------+------------------------------+------+
    

    Adding some print lines can help to understand things. Cheers!