0

I'm trying to extract a float from a line in a text file, but i can't for the life of me understand how to convert the number in the line (str) into a float

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

the attempted conversion with float(linevalue) is not going through, the output of the program remains e.g:

<type 'str'> 0.4323

Can anyone help me understand what am I missing ?

Thank you very much for your time.

Schwarz
  • 53
  • 7

1 Answers1

3

I think you want:

linevalue = float(linevalue)

You were correctly converting the string value to a float, but you weren't saving that value anywhere. (Calling float doesn't modify the existing variable; it returns a new value.)

user94559
  • 59,196
  • 6
  • 103
  • 103