[Tutor] Making table

János Juhász janos.juhasz at VELUX.com
Tue Mar 20 22:53:15 CET 2007


Dear Barry,

>>Using a formatting string of "%10.4f", these would be rendered as:
>>
>>               '  253.0000'
>>               '   77.6000
>>               '    9.0300'
>>               '    0.0210'
>>
>>This formatting gives the impression that all values but the last are
>>more precise than they truly are.  A scientist or statistician would
>>prefer to see something like this:
>>
>>               '254.    '
>>               ' 77.6   '
>>               '  9.03  '
>>               '  0.0210'
>>
>>Does numpy or some other math package have that capability?

You can use advanced regexp, called lookaround, lookbehind for this 
purpose.

###############

import re

l = (253., 77.6, 9.03, .0210, 1000, 100.1230)
ending_zero = re.compile('0(?=0*$)') # zero followed with only zeros

for f in l:
    print re.sub(ending_zero, ' ', ('%10.4f' % f))

###############




Yours sincerely,
______________________________
János Juhász
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070320/031db67e/attachment.htm 


More information about the Tutor mailing list