[Tutor] creating a regularly placed fields in a line

Alan Gauld alan.gauld at btinternet.com
Wed Apr 25 20:32:28 CEST 2012

On 25/04/12 16:17, Bala subramanian wrote:

> placed depending up on the size of the field. I am thinking to fix the
> size of each field.

> with open('tmp') as tp:
>      for line in tp:
>          bond.write('   %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) )

Add your widths to the format string:

          bond.write('   %10s%20s%30s%10s%25s\n' % (p1,p2,p3,p4,new) )

adjust the lengths to suit.

if you write the data in several places you can ensure consistency by 
defining the format string as a variable:

fmt = '   %10s%20s%30s%10s%25s\n'

bond.write( fmt % (p1,p2,p3,p4,new) )

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list