Search code examples
pythonpython-beautifultable

Convert rows of beautiful table to list


I would like to convert the rows of beautifultable to elements of a list while excluding the header e.g:

 [['A',2,4], ['B',2,5], ['C',2,1]]

Solution

  • Just call

    list(map(list, table))

    the full code:

    from beautifultable import BeautifulTable
    table = BeautifulTable()
    table.column_headers = ["c1", "c2", "c3"]
    table.append_row(['A', 2, 4])
    table.append_row(['B', 2, 5])
    table.append_row(['C', 2, 6])
    print(table)
    # it will print
    # +----+----+----+
    # | c1 | c2 | c3 |
    # +----+----+----+
    # | A  | 2  | 4  |
    # +----+----+----+
    # | B  | 2  | 5  |
    # +----+----+----+
    # | C  | 2  | 6  |
    # +----+----+----+
    li = list(map(list, table))
    print(li)
    # it will print
    # [['A', 2, 4], ['B', 2, 5], ['C', 2, 6]]