Python: Create A Box/rectangle/table Outside Of A Text?
I would like to have a dynamic/perfect box/rectangle/table outside of a text. In this example, I've a few variables arranged in 3 rows. The problem started when the length of the s
Solution 1:
Try tabulate module for automating the spacing in each cell. Try the following :
from tabulate import tabulate
n1 = 1
i1 = 'Apple'
d1 = 'Fruit'
n2 = 2
i2 = 'Antelope'
d2 = 'Animal'
n3 = 3
i3 = 'Afghanistan'
d3 = 'Country'
table_rows = [
['Number', 'Items', 'Description'],
[n1, i1, d1],
[n2, i2, d2],
[n3, i3, d3]
]
print(tabulate(table_rows, tablefmt="fancy_grid"))
Output :
╒════════╤═════════════╤═════════════╕
│ Number │ Items │ Description │
├────────┼─────────────┼─────────────┤
│ 1 │ Apple │ Fruit │
├────────┼─────────────┼─────────────┤
│ 2 │ Antelope │ Animal │
├────────┼─────────────┼─────────────┤
│ 3 │ Afghanistan │ Country │
╘════════╧═════════════╧═════════════╛
Solution 2:
You can use tabulate
module
Here is the code for the same
from tabulate import _table_formats, tabulate
table = [[1,"Apple","Fruit"], [2,"Antelope", "Animal"], [3,"Afghanistan", "Country"]]
headers = ["Number", "Items","Description"]
print(tabulate(table, headers, tablefmt="simple"))
Edit : Removed the link as it violated forum rules
Post a Comment for "Python: Create A Box/rectangle/table Outside Of A Text?"