mrzippy01
mrzippy01

Reputation: 383

Creating a table out of data in python

I am looking to create a table, with 4 columns.

In my program I have already formed a list with the information. I can split the data into chunks of 4 (for each line), or make a separate list for every column.

Is it possible to create a table including these values inside the python program or would I need to export the data first?

EDIT: For Fauxpas

Column 1                Column 2                Column 3                Column 4            

              10                      10                      10                      30

              20                      10                      10                      40

              20                      20                      10                      50

Upvotes: 0

Views: 1565

Answers (1)

Fauxpas
Fauxpas

Reputation: 86

If you're looking to do this text based you could use the .format method, read the docs to learn about formatting so you can specify the spacing of each column

like

your_list = ['bread', 'milk', 'sugar', 'tea']

print("{0:20}    {1:20}    {2:20}    {3:20}\n".format('Column 1', 'Column 2', 'Column 3', 'Column 4'))
print("{0:20}    {1:20}    {2:20}    {3:20}\n".format(your_list[0], your_list[1], your_list[2], your_list[3]))

Returns:

Column 1                Column 2                Column 3                Column 4            

bread                   milk                    sugar                   tea   

With a for loop example getting closer to what you might be wanting to do:

your_list = ['bread', 'milk', 'sugar', 'tea', 'eggs', 'shampoo', 'clothes', 'tiger', 'beads', 'washing machine', 'juice', 'mixed herbs']

print("{0:20}    {1:20}    {2:20}    {3:20}\n".format('Column 1', 'Column 2', 'Column 3', 'Column 4'))
i = 0
for x in range(0, 3):
    print("{0:20}    {1:20}    {2:20}    {3:20}\n".format(your_list[i], your_list[i + 1], your_list[i + 2], your_list[i + 3]))
    i += 4

Output:

Column 1                Column 2                Column 3                Column 4            

bread                   milk                    sugar                   tea                 

eggs                    shampoo                 clothes                 tiger               

beads                   washing machine         juice                   mixed herbs    

EDIT:

your_list = ['10', 10, '20', 20]
print("{0:20}    {1:20}    {2:20}    {3:20}\n".format('Column 1', 'Column 2', 'Column 3', 'Column 4'))
print("{0:20}    {1:20}    {2:20}    {3:20}\n".format(your_list[0], your_list[1], your_list[2], str(your_list[3])))

Output:

Column 1                Column 2                Column 3                Column 4            

10                                        10    20                      20      

If you convert the integers in your list to Strings demonstrated by the last element in my format statement using the Str() method then this won't happen :)

str(your_list[3])

https://docs.python.org/2/library/string.html

Upvotes: 1

Related Questions