将指数转换为浮点数
这是我的代码,尝试将该行的第二个字段从指数转换为浮点数。
outputrrd = processrrd.communicate()
(output, error) = outputrrd
output_lines = output.split('\n')
for line in output_lines:
m = re.search(r"(.*): ", line)
if m != None:
felder = line.split(': ')
epoch = felder[0].strip(':')
utc = epoch2normal(epoch).strip("\n")
#print felder[1]
data = float(felder[1])
float_data = data * 10000000
print float_data
resultslist.append( utc + ' ' + hostname + ' ' + float_data)
但是,程序因以下错误而停止:
File "/opt/omd/scripts/python/livestatus/rrdfetch-convert.py", line 156, in <module>
data = float(felder[1])
ValueError: invalid literal for float(): 6,0865000000e-01
有人知道原因吗?
This is my code, trying to convert the second field of the line from exponential into float.
outputrrd = processrrd.communicate()
(output, error) = outputrrd
output_lines = output.split('\n')
for line in output_lines:
m = re.search(r"(.*): ", line)
if m != None:
felder = line.split(': ')
epoch = felder[0].strip(':')
utc = epoch2normal(epoch).strip("\n")
#print felder[1]
data = float(felder[1])
float_data = data * 10000000
print float_data
resultslist.append( utc + ' ' + hostname + ' ' + float_data)
But, the program stops with this error:
File "/opt/omd/scripts/python/livestatus/rrdfetch-convert.py", line 156, in <module>
data = float(felder[1])
ValueError: invalid literal for float(): 6,0865000000e-01
Does anyone know the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最简单的方法就是更换!一个简单的例子:
The easy way is replace! One simple example:
原因是
6,0865000000e-01
中使用了逗号。这不起作用,因为float()
不支持区域设置。有关详细信息,请参阅 PEP 331。尝试
locale.atof()
,或者将逗号替换为点。The reason is the use of comma in
6,0865000000e-01
. This won't work becausefloat()
is not locale-aware. See PEP 331 for details.Try
locale.atof()
, or replace the comma with a dot.float
值是正确的;您只需根据需要显示它,例如:The
float
value is correct; you simply need to display it as desired, e.g.:这对我有用,试试吧。
This work for me, try it out.
我认为这对你有用:
I think it is useful to you:
只需将字符串转换为浮点数即可:
Simply by casting string into float:
我在尝试从科学/指数表示法中的字符串转换为浮点数时遇到了类似的问题(如果太长,也可能导致指数表示法)
I had a similar issue trying to convert from string in scientific/exponential notation to a float number (that can result also in exponential notation if too long)