0

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mrzippy01
  • 383
  • 1
  • 3
  • 15
  • What kind of table are you talking about? text, database, html? – Pedru Feb 16 '16 at 11:36
  • Just one which could display numbers to a user which is easy to understand. 4 columns x 5 rows @Pedru – mrzippy01 Feb 16 '16 at 11:41
  • OK, you can either do it yourself like @Fauxpas suggetst or you can use some library like [texttable](https://github.com/foutaise/texttable/), there are many others. – Pedru Feb 16 '16 at 11:47
  • This SO question might also be helpful to print pretty ascii-like tables http://stackoverflow.com/questions/5909873/python-pretty-printing-ascii-tables – danidee Feb 16 '16 at 12:20

1 Answers1

1

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

Fauxpas
  • 86
  • 6
  • The columns won't align unless they have the exact same width. – Selcuk Feb 16 '16 at 11:27
  • Can you post your format statements? Make sure you put 4 spaces before the code and check the preview output. Do this by editing your original post – Fauxpas Feb 16 '16 at 12:14
  • @Fauxpas This works perfectly apart from the fact that the column titles and the data below them don't line up. Is there anyway to fix this? (Ive edited the original post) – mrzippy01 Feb 16 '16 at 12:14
  • I'm no expert but I think because you were passing integers, that they were becoming part of the formatting. Have added an edit for you. – Fauxpas Feb 16 '16 at 12:31
  • Thanks this is so helpful works just how I wanted it to. Would you mind quickly explaining how the this works – mrzippy01 Feb 16 '16 at 12:56
  • This is the best article I could find to explain it https://mkaz.github.io/2012/10/10/python-string-format/ – Fauxpas Feb 16 '16 at 13:07