[Tutor] Formatter

Bob Gailer ramrom@earthling.net
Sat Jan 18 12:43:01 2003


--=======5B0344B5=======
Content-Type: text/plain; x-avg-checked=avg-ok-B74577E; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

At 11:05 AM 1/18/2003 -0500, Pete Versteegen wrote:
>Does anyone know of an easy way to format a mixture of floats, integers,
>spaces, and text in well arranged columns?
>
>My thoughts wander towards the way the Fortran language does it.  I'm
>considered a function (class) that would be called as follows:
>format((a, alf, i), "f10.2, a10, 10x, i5")

def format(values, format):
   formatlist = format.split(',')
   sf = ''
   for formatitem in formatlist:
     type = formatitem[0]
     if type in 'fi':
       sf += '%' + formatitem[1:] + type
     if type == 'a':
       sf += ' '*int(formatitem[1:])
     elif formatitem[-1] == 'x':
       sf += '%' + formatitem[:-1] + 's'
   return sf % values
a = 5
alf = 'qwer'
i = 3
print format((a, alf, i), "f10.2,a10,10x,i5")

result:       5.00                qwer    3

For simplicity I removed the spaces between the fortran format items. A 
little more code would handle them.

Bob Gailer
mailto:ramrom@earthling.net
303 442 2625

--=======5B0344B5=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-B74577E
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.438 / Virus Database: 246 - Release Date: 1/7/2003

--=======5B0344B5=======--