Q:

return a list by reading a text file

I am reading a text file separated by space,the file looks like:
first line:hello    2928977    [1.2,9.7]
second line: apple    6723547    [8.2,3.1]
.... 
I want to return a list like[(hello,1.2,9.7),(apple,8.2,3.1),...] 
how do i do this?
I know the first step is to set an empty list and then do 
    output =[]
    inputfile = open('text.txt','r')
    for eachline in inputfile:
            l=line.strip()
            s=l.split(',')
            output.append((s[0],float(s[2][0]),float(s[2][1]))
    return output

but this doesn't work... it says invalid syntax....can someone help me?
also I tried with another file with this method, but it says strings cannot be converted into float... i can't get this working, I would really appreaciate your help!Thanks!!!!!!! 

A:

I would usually use regex for this sort of pattern matching to make sure that the elements within [..] are numbers. 
But to continue with what you're doing: 
#s='hello 2928977 [1.2,9.7]\napple 6723547 [8.2,3.1]\n'
#sp=s.splitlines()
with open(filename, "rb") as f:
    sp=f.readlines()

output=[]
for p in sp:
    words = p.split(" ")
    x,y=[float(x) for  x in words[2].strip("[|]").split(",")]
    output.append((words[0], x,y ) )
print(output)

