Extracting float from string

Erik Max Francis max at alcyone.com
Mon Jan 13 21:39:25 EST 2003


Spencer Ernest Doidge wrote:

> From a previous post,
> I got many good solutions to extracting an int from
> 'X= 445 A'.
> 
> Now what about this one?
> Somewhere (we don't know where) in this input string, we expect to
> find a float:
> ':X=  0.22456  A'
> 
> How can I suck the float out of this one?

Again, you're going to have to specify better the bounds of the problem.
There are many ways to do it -- a regular expression would be a piece of
cake -- but the best solution depends on the variation that you expect
to see.  Do you expect to always have whitespace between the number and
the following non-numeric data?  Do you expect multiple per line?  And
so on.

Give us a sampling of variations you expect to see and what you want out
of them, and then people will probably be able to narrow in on a
solution that fits best.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ If love be good, from whence cometh my woe?
\__/ Chaucer
    Crank Dot Net / http://www.crank.net/
 Cranks, crackpots, kooks, & loons on the Net.




More information about the Python-list mailing list