Reputation: 21231
I have a string and I want to extract the numbers from it. For example:
str1 = "3158 reviews"
print (re.findall('\d+', str1 ))
Output is ['4', '3']
I want to get 3158
only, as an Integer preferably, not as List.
Upvotes: 64
Views: 330333
Reputation: 53
Use this, THIS IS FOR EXTRACTING NUMBER FROM STRING IN GENERAL.
To get all the numeric occurences.
getting number from string, use list comprehension+isdigit()
test_string = "i have four ballons for 2 kids"
# list comprehension + isdigit() +split()
res = [int(i) for i in test_string.split() if i.isdigit()]
print("The numbers list is : "+ str(res))
To extract numeric values from a string in python
Find list of all integer numbers in string separated by lower case characters using re.findall(expression,string)
method.
Convert each number in form of string into decimal number and then find max of it.
import re
def extractMax(input):
# get a list of all numbers separated by lower case characters
# \d+ is a regular expression which means one or more digit
numbers = re.findall('\d+',input)
number = map(int,numbers)
return max(numbers)
if __name__=="__main__":
input = 'sting'
print extractMax(input)
Upvotes: 0
Reputation: 5061
You can filter
the string by digits using str.isdigit
method,
>>> int(filter(str.isdigit, str1))
3158
For Python3:
int(list(filter(str.isdigit, my_str))[0])
Upvotes: 113
Reputation: 8709
This code works fine. There is definitely some other problem:
>>> import re
>>> str1 = "3158 reviews"
>>> print (re.findall('\d+', str1 ))
['3158']
Upvotes: 41
Reputation: 61
Python 2.7:
>>> str1 = "3158 reviews"
>>> int(filter(str.isdigit, str1))
3158
Python 3:
>>> str1 = "3158 reviews"
>>> int(''.join(filter(str.isdigit, str1)))
3158
Upvotes: 6
Reputation: 105
Using a list comprehension and Python 3:
>>> int("".join([c for c in str1 if str.isdigit(c)]))
3158
Upvotes: 0
Reputation: 1213
You were quite close to the final answer. Your re.finadall
expression was only missing the enclosing parenthesis to catch all detected numbers:
re.findall( '(\d+)', str1 )
For a more general string like str1 = "3158 reviews, 432 users"
, this code would yield:
Output: ['3158', '432']
Now to obtain integers, you can map
the int
function to convert strings into integers:
A = list(map(int,re.findall('(\d+)',str1)))
Alternatively, you can use this one-liner loop:
A = [ int(x) for x in re.findall('(\d+)',str1) ]
Both methods are equally correct. They yield A = [3158, 432]
.
Your final result
for the original question would be first entry in the array A
, so we arrive at any of these expressions:
result = list(map(int,re.findall( '(\d+)' , str1 )))[0]
result = int(re.findall( '(\d+)' , str1 )[0])
Even if there is only one number present in str1
, re.findall
will still return a list, so you need to retrieve the first element A[0]
manually.
Upvotes: 8
Reputation: 4936
For python3
input_str = '21ddd3322'
int(''.join(filter(str.isdigit, input_str)))
> 213322
Upvotes: 2
Reputation: 27456
you can use the below method to extract all numbers from a string.
def extract_numbers_from_string(string):
number = ''
for i in string:
try:
number += str(int(i))
except:
pass
return number
(OR) you could use i.isdigit()
or i.isnumeric
(in Python 3.6.5 or above)
def extract_numbers_from_string(string):
number = ''
for i in string:
if i.isnumeric():
number += str(int(i))
return number
a = '343fdfd3'
print (extract_numbers_from_string(a))
# 3433
Upvotes: 0
Reputation:
Best for every complex types
str1 = "sg-23.0 300sdf343fc -34rrf-3.4r" #All kinds of occurrence of numbers between strings
num = [float(s) for s in re.findall(r'-?\d+\.?\d*', str1)]
print(num)
Output:
[-23.0, 300.0, 343.0, -34.0, -3.4]
Upvotes: 5
Reputation: 149736
To extract a single number from a string you can use re.search()
, which returns the first match (or None
):
>>> import re
>>> string = '3158 reviews'
>>> int(re.search(r'\d+', string).group(0))
3158
In Python 3.6+ you can also index into a match object instead of using group()
:
>>> int(re.search(r'\d+', string)[0])
3158
Upvotes: 7
Reputation: 486
Your regex looks correct. Are you sure you haven't made a mistake with the variable names? In your code above you mixup total_hotel_reviews_string
and str
.
>>> import re
>>> s = "3158 reviews"
>>>
>>> print(re.findall("\d+", s))
['3158']
Upvotes: 20
Reputation: 89
My answer does not require any additional libraries, and it's easy to understand. But you have to notice that if there's more than one number inside a string, my code will concatenate them together.
def search_number_string(string):
index_list = []
del index_list[:]
for i, x in enumerate(string):
if x.isdigit() == True:
index_list.append(i)
start = index_list[0]
end = index_list[-1] + 1
number = string[start:end]
return number
Upvotes: 1
Reputation: 19
I am a beginner in coding. This is my attempt to answer the questions. Used Python3.7 version without importing any libraries.
This code extracts and returns a decimal number from a string made of sets of characters separated by blanks (words).
Attention: In case there are more than one number, it returns the last value.
line = input ('Please enter your string ')
for word in line.split():
try:
a=float(word)
print (a)
except ValueError:
pass
Upvotes: 1
Reputation: 53
a = []
line = "abcd 3455 ijkl 56.78 ij"
for word in line.split():
try:
a.append(float(word))
except ValueError:
pass
print(a)
OUTPUT
3455.0 56.78
Upvotes: 1
Reputation: 1953
Above solutions seem to assume integers. Here's a minor modification to allow decimals:
num = float("".join(filter(lambda d: str.isdigit(d) or d == '.', inputString)
(Doesn't account for - sign, and assumes any period is properly placed in digit string, not just some english-language period lying around. It's not built to be indestructible, but worked for my data case.)
Upvotes: 4
Reputation: 79
There may be a little problem with code from Vishnu's answer. If there is no digits in the string it will return ValueError. Here is my suggestion avoid this:
>>> digit = lambda x: int(filter(str.isdigit, x) or 0)
>>> digit('3158 reviews')
3158
>>> digit('reviews')
0
Upvotes: 2
Reputation: 114461
If the format is that simple (a space separates the number from the rest) then
int(str1.split()[0])
would do it
Upvotes: 6