0

Given the following table. How would I turn it into a table in python using variable.center(), variable.rjust(), variable.ljust(), %f and any other descriptor

  Name           Age      Height    Pay Rate   Grade
----------- ---------- ---------- ---------- ----------
John             15.00       1.20       10.00      8.00
Robert           45.00       6.70       24.00     14.00
Duncan           10.00       5.40        5.00     10.00
Steven           30.00       6.50       30.00     16.00

I would like to create the above table using python, how would I go on to format the above into a table

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
don
  • 186
  • 2
  • 15
  • how does your list look like? – spiehr Feb 16 '14 at 23:33
  • Before you go any further, I'll suggest you use a dictionary instead of a list. More readable and less confusing as you start to populate the table. `people['John']['Height']` is more transparent than, say, `people[0][1]`. Then `for person in people: for attrib in attribs: print(people[person][attrib])` plus whatever formatting. – OJFord Feb 16 '14 at 23:46

1 Answers1

2

Some people pointed out your question may be a duplicate, but the answers in question look somewhat old... Since the introduction of the new string format mini-language, you can do this kind of thing using str.format alone.

Supposing your list looks like:

data = [
    ["John"  , 15.00, 1.20, 10.00,  8.00],
    ["Robert", 45.00, 6.70, 24.00, 14.00],
    ["Duncan", 10.00, 5.40,  5.00, 10.00],
    ["Steven", 30.00, 6.50, 30.00, 16.00],
]

You can use the format method:

# Headers, centered
print("{: ^15s} {: ^15s} {: ^15s} {: ^15s} {: ^15s}".format(
    "Name", "Age", "Height", "Pay Rate", "Grade")
)

# Dashes (not very readable, but I'm lazy)
print(" ".join([("-" * 15)] * 5))

# Lines
for line in data:
    print("{:15s} {: 15.2f} {: 15.2f} {: 15.2f} {: 15.2f}".format(*line))

The format specification is (see the format specification mini-language):

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  (pretty much like printf)

^ means centered.

Result should be like:

     Name             Age           Height         Pay Rate          Grade     
--------------- --------------- --------------- --------------- ---------------
John                      15.00            1.20           10.00            8.00
Robert                    45.00            6.70           24.00           14.00
Duncan                    10.00            5.40            5.00           10.00
Steven                    30.00            6.50           30.00           16.00

Making this shorter and more reusable is left as an exercise for you.

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • *“but the other questions are very old”* — The first one I linked is from October last year; that’s not old at all. And my answer there uses the new string formatter too. The benefit of these answers in those questions is that you don’t need to specify the format yourself. You just pass in the data. – poke Feb 17 '14 at 00:12
  • @poke: fair enough, even upvoted your answer. – Paulo Scardine Feb 17 '14 at 00:14
  • haha, thanks ;D Returned the favor because your answer is simple enough for OP’s particular case :) – poke Feb 17 '14 at 00:18
  • This works but we havent learned yet about .format I wanted to use for loop and %s, %f to create the table. Would this be accomplished? and if so how would I come to this? – don Feb 17 '14 at 00:29
  • @dolilmao: If this is homework you should update the question with the assignment. – Paulo Scardine Feb 17 '14 at 00:30
  • If it’s homework, you should try solving it yourself, and only ask for help when you have specific problems with your solution. – poke Feb 17 '14 at 00:35