从Python

时间:2018-03-10 16:36:20

标签: python string extract

我有两个字符串:

['Renewables\n', '17.9% (3,951 MW)\n']
['Solar\n', '27.4% (1,081 MW)\n', 'LATEST SYSTEM\n', 'GENERATION\n', '4,738 MW\n', 'THERMAL GENERATION\n', '(COAL, GAS, OTHER)\n', '54 %\n', 'RENEWABLE\n', 'GENERATION\n', '47.61 %\n']

但我想要的唯一数据是每个字符串末尾的百分比数值,例如17.9和47.61,我想取这些数字并在后续程序中使用它们,其结果将取决于哪个数字更大,因为它们是网络抓取工具的结果。

如何将这些值作为浮点数提取,以便我可以继续使用它们?

修改

为了说清楚,我只想要每个字符串的最后一个百分比值,不需要任何MW值或以前的百分比值。

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式:

import re
s = ['Renewables\n', '17.9% (3,951 MW)\n']
s1 = ['Solar\n', '27.4% (1,081 MW)\n', 'LATEST SYSTEM\n', 'GENERATION\n', '4,738 MW\n', 'THERMAL GENERATION\n', '(COAL, GAS, OTHER)\n', '54 %\n', 'RENEWABLE\n', 'GENERATION\n', '47.61 %\n']
final_results = [float(re.findall('\d+\.\d+(?=\%)|\d+\.\d+(?=\s\%)', i[-1])[0]) for i in [s, s1]]

输出:

[17.9, 47.61]

答案 1 :(得分:0)

这是一个没有正则表达式的解决方案,但它非常适合您的情况。

代码检查字符串中的%,如果找到则拆分并在此之前提取数字。

examples = ['Solar\n', '27.4% (1,081 MW)\n', 'LATEST SYSTEM\n', 'GENERATION\n', '4,738 MW\n', 'THERMAL GENERATION\n', '(COAL, GAS, OTHER)\n', '54 %\n', 'RENEWABLE\n', 'GENERATION\n', '47.61 %\n']
output = []

for each_string in examples:
    if "%" in each_string:
        number = each_string.split("%")[0].strip(" ")
        output.append(number)

#output = ['27.4', '54', '47.61']