python 2.7、刮痧。 import re findall,改变输出,只剩下浮点数,计算总和

python 2.7, scraping. import re findall, changing output so that only float number is left, calculating sum

这是来自抓取

import re
import urllib
from BeautifulSoup import BeautifulSoup

我有这样的输出(print numbers_in_mill.text// 9.27[7] 9.25[8] 10.17[9] 10.72[10]

如何将这些输出更改为 // 9.27 9.25 10.17 10.72

我要去掉括号+括号里的数字,那我要计算我所有的总和float..numbers_in_mill +=float()

使用正则表达式:

import re
l = ['9.27[7]', '9.25[8]',"10.17[9]", "10.72[10]"]
for i in l:
    print(re.search("\d*\.?\d+", i).group())

输出:

9.27
9.25
10.17
10.72

你可以试试:

data = ['9.27[7]', '9.25[8]',"10.17[9]", "10.72[10]"]

for d in data:
    print float(d.split("[")[0])

9.27
9.25
10.17
10.72