Please help with regular expression finding multiple floats
Cousin Stanley
cousinstanley at gmail.com
Thu Oct 22 19:42:32 EDT 2009
> I have text that looks like the following
> (but all in one string with '\n' separating the lines):
> ....
>
> I want to capture the two or three floating point numbers in each line
> and store them in a tuple.
> ....
> I have the regular expression pattern
> ....
Jeremy ....
For a non-regular-expression solution
you might consider something simlar to
the following ....
s = '''\
1.0000E-08 1.58024E-06 0.0048
1.0000E-07 2.98403E-05 0.0018
1.0000E-06 8.85470E-06 0.0026
1.0000E-05 6.08120E-06 0.0032
1.0000E-03 1.61817E-05 0.0022
1.0000E+00 8.34460E-05 0.0014
2.0000E+00 2.31616E-05 0.0017
5.0000E+00 2.42717E-05 0.0017
total 1.93417E-04 0.0012'''
l1 = s.split( '\n' )
l2 = [ ]
for this_row in l1[ : -1 ] :
temp = this_row.strip().split()
l2.append( [ float( x ) for x in temp ] )
last = l1[ -1 ].strip().split()[ 1 : ]
l2.append( [ float( x ) for x in last ] )
print
for this_row in l2 :
if len( this_row ) > 2 :
x , y , z = this_row
print ' %5.4e %5.4e %5.4e ' % ( x , y , z )
else :
x , y = this_row
print ' %5.4e %5.4e ' % ( x , y )
--
Stanley C. Kitching
Human Being
Phoenix, Arizona
More information about the Python-list
mailing list