0

I am trying to write a function that prints out a input file: "table.txt" as seen on the left:

enter image description here

into the format as seen to the right of the image.

I have tried:

f = open("table1.txt",'r')
for aline in f:
    values = aline.split(',')
    print('Team:',values[0],', Points:',values[1],', Diff:',int(values[2])-int(values[3]),'Goals:',values[2]) 
f.close()

Which outputs:

Team: FC Ingolstadt 04 , Points:  13 , Diff: -2 Goals:  4
Team: Hamburg , Points:  9 , Diff: -2 Goals:  8
Team: SV Darmstadt 98 , Points:  9 , Diff: -1 Goals:  8
Team: Mainz , Points:  9 , Diff: -3 Goals:  6
Team: FC Augsburg , Points:  4 , Diff: -5 Goals:  7
Team: Werder Bremen , Points:  6 , Diff: -5 Goals:  7
Team: Borussia Moenchengladbach , Points:  6 , Diff: -6 Goals:  9
Team: Hoffenheim , Points:  5 , Diff: -4 Goals:  8
Team: VfB Stuttgart , Points:  4 , Diff: -8 Goals:  9
Team: Schalke 04 , Points:  16 , Diff: 11 Goals:  14
Team: Hannover 96 , Points:  2 , Diff: -12 Goals:  6
Team: Borrusia Dortmund , Points:  16 , Diff: 11 Goals:  15
Team: Bayern Munich , Points:  18 , Diff: 16 Goals:  18
Team: Bayer Leverkusen , Points:  14 , Diff: 3 Goals:  11
Team: Eintracht Frankfurt , Points:  9 , Diff: 4 Goals:  13
Team: Hertha BSC Berlin , Points:  14 , Diff: 1 Goals:  5
Team: 1. FC Cologne , Points:  13 , Diff: 0 Goals:  10
Team: VfB Wolfsburg , Points:  14 , Diff: 4 Goals:  10

But how do you print the values so that the rows are numbered and the columns have the titles: "Team, Points, Diff, Goals"?

Newbie
  • 378
  • 2
  • 12
  • 23

2 Answers2

3

what output you are getting from above code? will you paste it here?

and If you are getting output with repeated column headers in each line then you have to just print the Table headers above the for loop and remove it from the inner print for loop.

f = open("table1.txt",'r')
print('Team \t Points \t Diff \t Goals')
a=1; 
for aline in f:
    values = aline.split(',')
    print(a, values[0], values[1], int(values[2])-int(values[3]), values[2]) 
    a++;
f.close()

may this help you..

Ram Jadhav
  • 99
  • 4
  • this should be a comment :) – The6thSense Oct 20 '15 at 06:41
  • If you want rows with numbers then just simple.. initialise a variable with value 1 and print it in the loop and increment it. – Ram Jadhav Oct 20 '15 at 06:53
  • Thank you! Is there any way to get the columns to line up? – Newbie Oct 20 '15 at 07:06
  • sys.stdout.write("%4s \t %-50s \t %3s \t %3s \t %3s \n" % (a, values[0], values[1], int(values[2])-int(values[3]), values[2])) You may try something like this.. please see by default alignment is right and if you want text as Left justified then use "-" before the no. specing. plz see the '%-50s' we want Team as Left justified so we put the '-' sign here. – Ram Jadhav Oct 20 '15 at 07:46
1

I'm not sure if this is exactly what you need, but I think the formatting you want might go like this:

contents = []
with open('tables.dat', 'r+') as f:
    for line in f:
        contents.append(line.split(','))

max_name_length = max([len(line[0]) for line in contents])

print("    Team                       Points     Diff      Goals     \n")
print("--------------------------------------------------------------------------\n")
for i, line in enumerate(contents):
    line = [el.replace('\n', '') for el in line]
    print("{i:>3}  {0:<{fill_width}}   {1:>3}   {2:>3}   {3:>3}".format(i=i, *line, fill_width=max_name_length))

And here's some sample output:

    Team                       Points     Diff      Goals     

--------------------------------------------------------------------------

  0  FC Ingolstadt 04             13    -2     4
  1  Hamburg                       9    -2     8
  2  SV Darmstadt 98               9    -1     8
  3  Mainz                         9    -3     6
  4  FC Augsburg                   4    -5     7
  5  Werder Bremen                 6    -5     7
  6  Borussia Moenchengladbach     6    -6     9
  7  Hoffenheim                    5    -4     8
  8  VfB Stuttgart                 4    -8     9
  9  Schalke 04                   16    11    14
 10  Hannover 96                   2   -12     6
 11  Borrusia Dortmund            16    11    15
 12  Bayern Munich                18    16    18
 13  Bayer Leverkusen             14     3    11
 14  Eintracht Frankfurt           9     4    13
 15  Hertha BSC Berlin            14     1     5
 16  1. FC Cologne                13     0    10
 17  VfB Wolfsburg                14     4    10

EDIT 1: After updated question info

EDIT 2: fill width specification for Goals is not needed (it's the last)

EDIT 3: Points, diffs, and goals values centered. NOTE: I did it manually by experimentation. You'll have to do some max_name_length kind of thing if the values' length will go beyond 2 digits.

EDIT 4: Updated after seeing how your sample output looks like.

bad_keypoints
  • 1,382
  • 2
  • 23
  • 45
  • Thanks that works better. But do you see the intended output on the image? Every row is numbered and the three columns have titles at the top. How would I do this? – Newbie Oct 20 '15 at 07:04
  • How would I change the code so that the titled rows start at "1" instead of "0"? – Newbie Oct 20 '15 at 21:46
  • Just put i= i+1 in the key area. To answer my own question :) – Newbie Oct 21 '15 at 23:19