Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a number from a 1-word string

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.

like image 344
Kyle Avatar asked Apr 05 '12 23:04

Kyle


People also ask

How do you find a number in a string?

To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.

How do I extract numbers from a word in Python?

Making use of isdigit() function to extract digits from a Python string. Python provides us with string. isdigit() to check for the presence of digits in a string. Python isdigit() function returns True if the input string contains digit characters in it.

How do I find the numeric value of a string in Python?

Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values.


2 Answers

>>> 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'
like image 163
Nolen Royalty Avatar answered Sep 16 '22 18:09

Nolen Royalty


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
like image 21
Joel Cornett Avatar answered Sep 20 '22 18:09

Joel Cornett