2016-07-13 45 views
0

我想从文本文件中的行中提取一个浮点数,但我不能为我的生活了解如何将行(str)中的数字转换为浮点数Python字符串浮动

for line in fh: 
    if not line.startswith("A specific string") : continue 
    count = count + 1 
#linevalue gets the numbers in the line that i'm interested in, so in order 
#to have only the numeric value i tried getting rid of the other text in the line by only 
# selecting that range (20:26 
    linevalue = line[20:26] 
    float(linevalue) 
    print type(linevalue) 
    print linevalue 

尝试的转换与浮动(linevalue)是不会通过,程序的输出保持如:

<type 'str'> 0.4323 

谁能帮助我明白我失去了什么?

非常感谢您的宝贵时间。

回答

3

我想你想:

linevalue = float(linevalue) 

你是字符串值正确地转换为float,但你不保存该值的任何地方。 (调用float不会修改现有的变量;它会返回一个新值。)

+0

非常感谢你...我现在觉得哑巴:)我完全错过了 – Schwarz