advanced number recognition in strings?

Roy Smith roy at
Mon May 8 19:16:20 CEST 2006

s.schmeier at <s.schmeier at> wrote:
>we want extract numbers from strings and wonder if there is already a
>module around for doing this. An example in our case would look like
>0.032 +/- 0.5 x 10(-4)
>it would even be helpful to have a routine which does not recognise the
>+/- , but at least the 10(-4).

Is there some reason the numbers need to be written in that exact
format?  People have been writing machine-parsable floating point
numbers for eons, using the e-notation, and Python is happy to convert
a string in e-notation directly to a float:

>>> float ("0.032e-4")

That being said, e-notation does not represent uncertainty, but then
again, neither does an IEEE float.  To keep the uncertainty, you need
to create a new numeric class which stores the mantissa, the
uncertainty, and the exponent.  Perhaps you could get away with
storing the mantissa and exponent as a standard float, but you'd still
need to store the uncertainty.  Then you'd (presumably) need to define
a whole set of math operators which did something useful with these
numbers.  For example, what should:

x = 0.032 +/- 0.5 x 10(-4)
y = 0.032 +/- 1.0 x 10(-4)
print x == y

print out?

More information about the Python-list mailing list