I am new to Python
and I have a String, I want to extract the numbers from the string. For example:
我是Python新手,我有一个字符串,我想从字符串中提取数字。例如:
str1 = "3158 reviews"print (re.findall('\d+', str1 ))
Output is ['4', '3']
输出是[' 4 ',' 3 ')
I want to get 3158
only, as an Integer preferably, not as List.
我只想得到3158,最好是整数,而不是列表。
6 个解决方案
#1
53
You can filter
the string by digits using str.isdigit
method,
可以使用string .isdigit方法对字符串进行筛选,
>>> int(filter(str.isdigit, str1))3158
#2
15
This code works fine. There is definitely some other problem:
这段代码工作正常。肯定还有其他问题:
>>> str1 = "3158 reviews">>> print (re.findall('\d+', str1 ))['3158']
#3
6
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
.
你的正则表达式是正确的。你确定变量名没有出错吗?在您上面的代码中,混合了total_hotel_reviews_string和str。
>>> import re>>> s = "3158 reviews">>> print re.findall("\d+", s)['3158']
#4
5
If the format is that simple (a space separates the number from the rest) then
如果格式很简单(空格将数字与其他数字分开),那么
int(str1.split()[0])
would do it
会这么做
#5
1
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
#6
0
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:
Vishnu的答案可能会有一点问题。如果字符串中没有数字,它将返回ValueError。我的建议是:
>>> digit = lambda x: int(filter(str.isdigit, x) or 0)>>> digit('3158 reviews')3158>>> digit('reviews')0
#1
53
You can filter
the string by digits using str.isdigit
method,
可以使用string .isdigit方法对字符串进行筛选,
>>> int(filter(str.isdigit, str1))3158
#2
15
This code works fine. There is definitely some other problem:
这段代码工作正常。肯定还有其他问题:
>>> str1 = "3158 reviews">>> print (re.findall('\d+', str1 ))['3158']
#3
6
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
.
你的正则表达式是正确的。你确定变量名没有出错吗?在您上面的代码中,混合了total_hotel_reviews_string和str。
>>> import re>>> s = "3158 reviews">>> print re.findall("\d+", s)['3158']
#4
5
If the format is that simple (a space separates the number from the rest) then
如果格式很简单(空格将数字与其他数字分开),那么
int(str1.split()[0])
would do it
会这么做
#5
1
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
#6
0
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:
Vishnu的答案可能会有一点问题。如果字符串中没有数字,它将返回ValueError。我的建议是:
>>> digit = lambda x: int(filter(str.isdigit, x) or 0)>>> digit('3158 reviews')3158>>> digit('reviews')0