Extracting a number from a 1-word string

Go To StackoverFlow.com

5

In this program I am trying to make, I have an expression (such as "I=23mm", or "H=4V") and I am trying to extract the 23 (or the 4) out of it, so that I can turn it into an integer.

The problem I keep running into is that since the expression I am trying to take the numbers out of is 1 word, I cannot use split() or anything.

One example I saw but wouldnt work was -

I="I=2.7A"
[int(s) for s in I.split() if s.isdigit()]

This wouldnt work because it only takes the numbers are are delimited by spaces. If there was a number in the word int078vert, it wouldnt extract it. Also, mine doesnt have spaces to delimit.

I tried one that looked like this,

re.findall("\d+.\d+", "Amps= 1.4 I")

but it didnt work either, because the number that is being passed is not always 2 digits. It could be something like 5, or something like 13.6.

What code do I need to write so that if I pass a string, such as

I="I=2.4A"

or

I="A=3V"

So that I can extract only the number out of this string? (and do operations on it)? There are no spaces or other constant chars that I can delimit by.

2012-04-05 23:11
by Kyle
It looks like you are trying to solve this for both integer and decimal numbers. Will each string always have exactly one number - yoozer8 2012-04-05 23:16
Yeah. Each string will always have 1 number, but might have multiple decimals to make that number - Kyle 2012-04-06 02:10


11

>>> import re
>>> I = "I=2.7A"
>>> s = re.search(r"\d+(\.\d+)?", I)
>>> s.group(0)
'2.7'
>>> I = "A=3V"
>>> s = re.search(r"\d+(\.\d+)?", I)
>>> s.group(0)
'3'
>>> I = "I=2.723A"
>>> s = re.search(r"\d+(\.\d+)?", I)
>>> s.group(0)
'2.723'
2012-04-05 23:19
by Nolen Royalty
Thanks a lot. Worked well - Kyle 2012-04-06 02:10


3

RE is probably good for this, but as one RE answer has already been posted, I'll take your non-regex example and modify it:


One example I saw but wouldnt work was - 

I="I=2.7A"
[int(s) for s in I.split() if s.isdigit()]

The good thing is that split() can take arguments. Try this:

extracted = float("".join(i for i in I.split("=")[1] if i.isdigit() or i == "."))

Incidentally, here's a breakdown of the RE you supplied:

"\d+.\d+"
\d+ #match one or more decimal digits
. #match any character -- a lone period is just a wildcard
\d+ #match one or more decimal digits again

One way to do it (correctly) would be:

"\d+\.?\d*"
\d+ #match one or more decimal digits
\.? #match 0 or 1 periods (notice how I escaped the period)
\d* #match 0 or more decimal digits
2012-04-06 00:38
by Joel Cornett
Your split solution is quite neat :D. + - Nolen Royalty 2012-04-06 01:08
Appreciate the different method. + - Kyle 2012-04-06 02:09
Ads