【发布时间】:2013-08-09 17:43:25
【问题描述】:
我正在尝试从文本文件的行中提取科学数字。类似的东西
例子:
str = 'Name of value 1.111E-11 Next Name 444.4'
结果:
[1.111E-11, 444.4]
我已经在其他帖子中尝试过解决方案,但看起来这只适用于整数(也许)
>>> [int(s) for s in str.split() if s.isdigit()]
[]
float() 可以工作,但每次使用字符串时都会出错。
>>> float(str.split()[3])
1.111E-11
>>> float(str.split()[2])
ValueError: could not convert string to float: value
提前感谢您的帮助!!
【问题讨论】:
-
将变量命名为
str是不明智的,因为它会覆盖内置变量。 -
哦,是的,智慧之语。多少次我不得不对这样做的mo同事生气:P
标签: python string floating-point scientific-notation