0

In Python if I have numerous lines of data containing both strings and floats (sample below) which I have tokenized, how can I call the first float value in each line if this position is not constant? I eventually want to use this as a reference point for latter tokens. Thanks in advance.

F + FR > FR* + F + E 11.60 0 2 FR > FR*

F + FR > FR*** + F 11.60 0 2382 FR > FR***

MPelletier
  • 16,256
  • 15
  • 86
  • 137
  • Please show the sample data – thefourtheye Mar 17 '14 at 15:53
  • And what do you mean by "call"? Numbers are not callable (you can't say `123()`), do you want to store it in a variable, or print it? – tripleee Mar 17 '14 at 15:55
  • Not worthy of a full answer, but generally after tokenizing you would convert the individual tokens into a parse tree, then you would traverse the parse tree to interpret the statement. Read up on PLY or ANTLR or any other lexer/parser type subsystems. – Joe Holloway Mar 17 '14 at 16:03

1 Answers1

0

You can use the re module. Just import it and look for digits with a . within them. For example,

import re
def findFloat(s): 
    return float( re.search('[0-9]+.[0-9]+', s).group() )

This finds the first occurrence of a group of numbers separated by a .

ssm
  • 5,277
  • 1
  • 24
  • 42