Python在特定子字符串之前提取一个十进制数字

时间:2019-07-04 08:19:47

标签: python regex string list-comprehension

我想在特定子字符串(“百分比”)之前提取一个数字

我尝试使用split函数

str1="The percentage of success for Team A is around 7.5 per cent. What about their season ?"
print(str1.split("per cent",1)[0])

预期结果:"7.5"

实际结果:"The percentage of success for Team A is around 7.5"

4 个答案:

答案 0 :(得分:4)

您可以使用str.index找到发生per cent的索引,将字符串切成最终的索引,然后rstripsplit保留最后一个元素结果列表:

str1[:str1.index('per cent')].rstrip().split()[-1]
# '7.5'

答案 1 :(得分:2)

您可以为此使用正则表达式:

import re

str1="The percentage of success for Team A is around 7.5 per cent. What about their season ?"

m = re.search('([0-9.-]+) per cent', str1)
m[1]
=>7.5

我的工作如下:我创建了一个正则表达式,它匹配数字,破折号和点的任何组合(以粗略地匹配可能为负的数字),后跟确切的文本per cent

我将数字指定为一个组,因此您可以通过访问找到的匹配项的第1个索引来获取它。

答案 2 :(得分:1)

我将介绍4种情况:A)仅使用.表示的正小数,B)使用.表示的任何小数,C)使用.表示的多个小数,D )使用.,表示的多个小数。

A)假设您的浮点数始终以十进制表示法

import re

results = re.findall("\d+\.\d+",str1)[0]
print(results)
#'7.5'

B)如果您还有 NEGATIVE 个小数,请使用此(更可靠):

results = re.findall(r"[-+]?\d*\.\d+|\d+",str1)

C)如果您有多个小数,请使用此

str1="The percentage of success for Team A is around 7.5 per cent and 2.3"

results = re.findall(r"[-+]?\d*\.\d+|\d+",str1)

len(results)
#2 since it found the 2 decimals.

# Use list comprehension to store the detected decimals.
final_results = [i for i in results]
print(final_results)
#['7.5', '2.3']

D)最后,如果使用.(点)或,(逗号)来表示小数,则使用超级健壮性:

str1="The percentage of success for Team A is around 7.5 per cent and 2,3"

results = re.findall(r"\d+[.,]*\d*[.,]*\d*",str1)
final_results = [i for i in results]
#['7.5', '2,3']

答案 3 :(得分:0)

str1.split('per cent')[0].split(' ')[-2]