[Tutor] Formatter
Pete Versteegen
pversteegen@gcnetmail.net
Sat Jan 18 14:44:02 2003
On 1/18/03 12:42 PM, "Bob Gailer" <ramrom@earthling.net> wrote:
> 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
>
>
> ---
> 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
>