如何编写正则表达式来获取Python中的浮点数?

Ran*_*ndi 0 python regex

如何编写正则表达式来获取python中的浮点数.我想得到55.97.来自<td nowrap="nowrap">55.97</td>.所以我给了

newsecond_row_data = (re.search('(?<=>)\d+|\d+.\d+',second_row_data[a]))
newsecond_row_data.group(0)

print newsecond_row_data.group(0)
Run Code Online (Sandbox Code Playgroud)

但它给了55而不是55.97.Plz hlp我

谢谢

Sud*_*eer 7

如果要从HTML或XML中提取数据,请查看可用的各种解析器.对于这种特殊情况,您可以非常轻松地提取数字:

>>> from xml.etree import ElementTree
>>> element = ElementTree.fromstring('<td nowrap="nowrap">55.97</td>')
>>> element.text
'55.97'
>>> 
Run Code Online (Sandbox Code Playgroud)

  • 如果不适用,则不使用正则表达式+1 (2认同)