Converting exponential to float

发布于 2021-01-29 14:09:48

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?

关注者
0
被浏览
125
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    The easy way is replace! One simple example:

    value=str('6,0865000000e-01')
    value2=value.replace(',', '.')
    float(value2)
    0.60865000000000002
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看