Extracting a number from a 1-word string
- by Kyle
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.