[Tutor] Printing columns of data

Alan Gauld alan.gauld at freenet.co.uk
Tue Feb 8 23:02:01 CET 2005


> So I wrote the program included below and it only prints the last
line
> of the file.

> I have one question. Do I need to put ts and pe into a list before I
> print then to screen or I am just missing something. Thanks.

You just need to indent your last print statement so it is inside
the loop and put the heading print statement before the loop.

print """

Timestep    PE"""

for line in cols:
    ts = line[0]
    pe = line[1]
    print "%s      %s     " % (ts,pe)

You might also like to use stroing formatting to force column
widths to be constant:

    print "%20s%20s" % (ts,pe)

should illustrate...adjust the size to suit.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list