Search code examples
pythonpython-3.xprettytable

Python Add data from array to prettytable via column


I created an array full of number. I want to add data from the array to the prettytable basically using add_columns

number=[1,2,3,4,5,6,7,8,9,.....,100]

I want the output of the pretty table to be like this below

+-----------+------+------------+-----------------+
| Number  |1 | 2 | 3 | 4 | .....                  |
+-----------+------+------------+-----------------+

My code is shown below.

from prettytable import PrettyTable

x = PrettyTable()
x.add_column(["number", print(number)])
print(x)

When I run the python script, it generated an error

TypeError: add_column() missing 1 required positional argument: 'column'

How to achieve this?


Solution

  • you should use add raw like this and if you need to add 'number' as text you should added in the list

    number=['number',1,2,3,4,5,6,7,8,9]
    
    from prettytable import PrettyTable
    x = PrettyTable()
    x.add_row(number)
    print(x)
    

    OUTPUT:

    +---------+---------+---------+---------+---------+---------+---------+---------+---------+----------+
    | Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | Field 7 | Field 8 | Field 9 | Field 10 |
    +---------+---------+---------+---------+---------+---------+---------+---------+---------+----------+
    |  number |    1    |    2    |    3    |    4    |    5    |    6    |    7    |    8    |    9     |
    +---------+---------+---------+---------+---------+---------+---------+---------+---------+----------+