2

I'm new to python and my problem seems to be so stupid I can't even find an answer when I google it...

I'm writing a program that uses data on different planets and their satellites, makes some calculations to get the mass of the planets, and is then supposed to present the calculated data in a table.

The table is my problem. I'm using lists, but when I print the table I created it looks terrible.

Here is my code so far:

table = [["Typ av data", "Jupiter", "Saturnus", "Solen"],
     ["Planetmassa", jupiter_massa, saturnus_massa, solen_massa],
     ["Standardavvikelse", jupiter_standardavvikelse, saturnus_standardavvikelse, solen_standardavvikelse]]
for row in table:
   print(row)

(It's in swedish but that shouldn't matter. The words with "" are just words/strings, and the rest are lists containing the data.)

This is what it looks like when I print it:

['Typ av data', 'Jupiter', 'Saturnus', 'Solen']
['Planetmassa', 4.110628390867864e+39, 1.2347845577268216e+39, 4.319543783963521e+42]
['Standardavvikelse', 2.3357256803779513e+37, 8.050789009367835e+36, 1.9328559536580606e+40]

If possible I want to get rid of the brackets and the '', but most importantly - I want it to be displayed as a proper table with a fixed column width. Like I said I have googled but can't find a single answer (maybe I'm just stupid). I have also tried to install modules like prettytable and texttable, but it doesn't seem to work on my computer so I need to work with what already exists.

So - how do I fix the column width?

I hope this makes sense and that someone can help me cause I'm going mad. If you need any more information please let me know.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2254681
  • 21
  • 1
  • 2

2 Answers2

2

First of all, printing a list will show you the representation of that list.

>>> l = ['1', '2', '3']
>>> print l
['1', '2', '3']

To print the items separately, you can do multiple things.

You can print them each on their own line,

>>> for i in l:
    print i

1
2
3

Or you can format them with a string format.

>>> print "%s" % ', '.join(l)
1, 2, 3

Otherwise, if you want to create tables automatically you can use some modules like CSV or prettytable.

Example of prettytable from the websites:

PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables, like this:

+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide  | 1295 |  1158259   |      600.5      |
| Brisbane  | 5905 |  1857594   |      1146.4     |
| Darwin    | 112  |   120900   |      1714.7     |
| Hobart    | 1357 |   205556   |      619.5      |
| Melbourne | 1566 |  3806092   |      646.9      |
| Perth     | 5386 |  1554769   |      869.4      |
| Sydney    | 2058 |  4336374   |      1214.8     |
+-----------+------+------------+-----------------+

PrettyTable lets you control many aspects of the table, like the width of the column padding, the alignment of text within columns, which characters are used to draw the table border, whether you even want a border, and much more. You can control which subsets of the columns and rows are printed, and you can sort the rows by the value of a particular column.

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • Thanks a lot! I've tried several times to install prettytable because that is exactly the kind of module I want to use, but i can't get it to work on my computer... – user2254681 Apr 07 '13 at 14:43
  • That is a problem that you can take up on a different website. However, if you don't mind creating a file with your results instead of printing, you can use the pre-shipped CSV module that comes with python, or you can implement the whole thing on your own. – Inbar Rose Apr 07 '13 at 14:44
0

Make a simple table

from beautifultable import BeautifulTable


table = BeautifulTable()
table.column_headers = ["", 'Jupiter', 'Saturnus', 'Solen']
table.append_row(['Planetmassa', round(4.110628390867864,3),  round(1.2347845577268216,3),  round(4.319543783963521,3)])
table.append_row(['Standardavvikelse', 1,2, 7])

print(table)

you will get it

Sample Planets Table

jtessier72
  • 412
  • 3
  • 8
Wojciech Moszczyński
  • 2,893
  • 21
  • 27