ValueError: invalid literal for float(): Timestep:
Question: I have a program that finds some data and runs a basic math function on the
data, but when I run it I get the following error: `ValueError: invalid
literal for float(): Timestep:`. The error occurs in line where I call
`map(float,line.split()[1:])`.

Does anyone know why and how to fix this error.

    
    
    #!/usr/bin/python
    
    
    l=[]
    with open("movie.xyz") as f:
       line = f.next()
       nat = int(line.split()[0])
       print nat
       f.next()# skip headers
       for line in f:
           if line.strip():
               l.append(map(float,line.split()[1:]))  # make all values floats   
    #print l[0][0]
    b = 0
    a = 1
    for b in range(55):
        for a in range(b+1,56):
        import operator
        import numpy as np
        #vector1 = l[b]
        vector1 = (l[b][0],l[b][1],l[b][2])
        vector2 = (l[a][0],l[a][1],l[a][2])
        #print('vector 1 = %' % vector1)
        #print('vector 1 = (%f,%f,%f)' % vector1)
        #print vector2
        x = vector1
        y = vector2
        vector3 = list(np.array(x) - np.array(y))
        #print vector3
        dotProduct = reduce( operator.add, map( operator.mul, vector3, vector3))    
        dp = dotProduct**.5
        print dp
    

first couple lines of data look like:

    
    
    2805
    Atoms. Timestep: 0
    Cu 46.7176 27.1121 27.1121
    Cu 43.2505 36.0618 32.4879
    Cu 43.3392 36.0964 28.9236
    Cu 43.2509 37.8362 27.1091
    Cu 43.3406 36.0958 25.2957
    Cu 43.2582 36.0629 21.737
    Cu 43.2505 32.4879 36.0618
    


Answer: insert

    
    
    import pdb; pdb.set_trace()
    

before this line and see what gives you `line.split()[1:]`

pdb = prompt debugger: <https://docs.python.org/2/library/pdb.html>

or do this:

    
    
    if line.strip():
        try:
            l.append(map(float,line.split()[1:]))  
        except ValueError:
            print "Value error at: ", line.split()[1:]
    

## #

to make this code work add this function:

    
    
    def foo(value):
        try:
            result = float(value)
        except ValueError:
            print "cant parse %r into float" %value
            result = None
        return result
    

and replace the line:

    
    
    l.append(map(float,line.split()[1:]))
    

to:

    
    
    l.append(map(foo, line.split()[1:]))
    

