Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

python - Converting exponential to float

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('
')
for line in output_lines:
    m = re.search(r"(.*): ", line)
    if m != None:
        felder = line.split(': ')
        epoch =  felder[0].strip(':')
        utc = epoch2normal(epoch).strip("
")
        #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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easy way is replace! One simple example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...