【问题标题】:python 2.7, scraping. import re findall, changing output so that only float number is left, calculating sumpython 2.7,抓取。 import re findall,改变输出,只剩下浮点数,计算总和
【发布时间】:2018-05-07 06:01:00
【问题描述】:

这是来自抓取

import re
import urllib
from BeautifulSoup import BeautifulSoup

我有类似的输出(打印 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()的总和

【问题讨论】:

  • 你能提供更多代码和解释你是如何得到这些输出的吗?

标签: python output screen-scraping webpage


【解决方案1】:

使用正则表达式:

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

【讨论】:

  • 太棒了。谢谢你们两位的快速回答。你帮了我很多。
【解决方案2】:

你可以试试:

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

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多