这是我的代码,试图将行的第二个字段从指数转换为浮点。
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
有人知道原因吗?
简单的方法是更换!一个简单的例子:
value=str('6,0865000000e-01') value2=value.replace(',', '.') float(value2) 0.60865000000000002