Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Query about doing fortran-esque repeat formatting

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


rdbriggs at mun

Nov 8, 2009, 6:56 AM

Post #1 of 6 (86 views)
Permalink
Query about doing fortran-esque repeat formatting

Hello,

Is there a way to do a repeat formatting command like in Fortran? Rather
that doing this:

print "%s %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f" %
(parmName[i], tmp[i][1], tmp[i][2], tmp[i][4], tmp[i][6], tmp[i][7],
tmp[i][8], tmp[i][9])

Something like this:

print "%s 7%-5.3f % (parmName[i], tmp[i][1], tmp[i][2], tmp[i][4],
tmp[i][6], tmp[i][7], tmp[i][8], tmp[i][9])

regards,

Rob

--
http://mail.python.org/mailman/listinfo/python-list


mensanator at aol

Nov 8, 2009, 8:08 AM

Post #2 of 6 (85 views)
Permalink
Re: Query about doing fortran-esque repeat formatting [In reply to]

On Nov 8, 8:56�am, Rob Briggs <rdbri...@mun.ca> wrote:
> Hello,
>
> Is there a way to do a repeat formatting command like in Fortran? Rather
> that doing this:
>
> print "%s %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f" %
> (parmName[i], tmp[i][1], tmp[i][2], tmp[i][4], �tmp[i][6], �tmp[i][7],
> tmp[i][8], �tmp[i][9])
>
> Something like this:
>
> print "%s 7%-5.3f % (parmName[i], tmp[i][1], tmp[i][2], tmp[i][4],
> tmp[i][6], �tmp[i][7], tmp[i][8], �tmp[i][9])
>

>>> s = '%s ' + ' %-5.4f' * 7
>>> s
'%s %-5.4f %-5.4f %-5.4f %-5.4f %-5.4f %-5.4f %-5.4f'
>>> print s % ('s',1,2,3,4,5,6,7)
s 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000




> regards,
>
> Rob

--
http://mail.python.org/mailman/listinfo/python-list


gregory.j.baker at gmail

Nov 8, 2009, 4:56 PM

Post #3 of 6 (78 views)
Permalink
Re: Query about doing fortran-esque repeat formatting [In reply to]

How about:

print ('%s ' + '%-5.4f ' * 7) % ('text',1,2,3,4,5,6,7)

--
http://mail.python.org/mailman/listinfo/python-list


zondo42 at googlemail

Nov 9, 2009, 4:05 AM

Post #4 of 6 (70 views)
Permalink
Re: Query about doing fortran-esque repeat formatting [In reply to]

Rob Briggs <rdbriggs <at> mun.ca> writes:

> Is there a way to do a repeat formatting command like in Fortran? Rather
> that doing this:
>
> print "%s %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f" %
> (parmName[i], tmp[i][1], tmp[i][2], tmp[i][4], tmp[i][6], tmp[i][7],
> tmp[i][8], tmp[i][9])

There certainly is. You can use python's string concatenation
and repeat operators:

print "%s" + " %-5.3f" * 7 % <stuff>

Glenn

--
http://mail.python.org/mailman/listinfo/python-list


jeanmichel at sequans

Nov 9, 2009, 4:31 AM

Post #5 of 6 (68 views)
Permalink
Re: Query about doing fortran-esque repeat formatting [In reply to]

Glenn Hutchings wrote:
> Rob Briggs <rdbriggs <at> mun.ca> writes:
>
>
>> Is there a way to do a repeat formatting command like in Fortran? Rather
>> that doing this:
>>
>> print "%s %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f" %
>> (parmName[i], tmp[i][1], tmp[i][2], tmp[i][4], tmp[i][6], tmp[i][7],
>> tmp[i][8], tmp[i][9])
>>
>
> There certainly is. You can use python's string concatenation
> and repeat operators:
>
> print "%s" + " %-5.3f" * 7 % <stuff>
>
> Glenn
>
>

data = tuple(parmName[i]) + tuple(tmp[i])

print "%s" + " %-5.3f" * len(tmp[i]) % data

That should do the trick.

JM


rdbriggs at mun

Nov 9, 2009, 6:53 AM

Post #6 of 6 (67 views)
Permalink
Re: Query about doing fortran-esque repeat formatting [In reply to]

Thanks to the chaps who answered,

I knew there would be an efficient answer to this.

regards,

Rob


On Mon, 2009-11-09 at 13:31 +0100, Jean-Michel Pichavant wrote:
> Glenn Hutchings wrote:
> > Rob Briggs <rdbriggs <at> mun.ca> writes:
> >
> >
> > > Is there a way to do a repeat formatting command like in Fortran? Rather
> > > that doing this:
> > >
> > > print "%s %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f %-5.3f" %
> > > (parmName[i], tmp[i][1], tmp[i][2], tmp[i][4], tmp[i][6], tmp[i][7],
> > > tmp[i][8], tmp[i][9])
> > >
> >
> > There certainly is. You can use python's string concatenation
> > and repeat operators:
> >
> > print "%s" + " %-5.3f" * 7 % <stuff>
> >
> > Glenn
> >
> >
>
> data = tuple(parmName[i]) + tuple(tmp[i])
> print "%s" + " %-5.3f" * len(tmp[i]) % data
>
> That should do the trick.
>
> JM

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.