0

Program Details:

I am writing a program for python that will need to look through a text file for the line:

Found mode 1 of 12: EV= 1.5185449E+04, f= 19.612545, T= 0.050988.

Problem:

Then after the program has found that line, it will then store the line into an array and get the value 19.612545, from f = 19.612545.

Question:

I so far have been able to store the line into an array after I have found it. However I am having trouble as to what to use after I have stored the string to search through the string, and then extract the information from variable f. Does anyone have any suggestions or tips on how to possibly accomplish this?

4
  • docs.python.org/library/re.html may be what you are looking for. Commented Jul 27, 2012 at 18:43
  • @CosmicComputer he should be using regular string parsing functions here, regular expressions are overkill Commented Jul 27, 2012 at 18:44
  • I am thinking I would possible use a regular expression that says: f = (any possible combination of numbers from 0-9) Commented Jul 27, 2012 at 18:44
  • he already got those strings... he now wants to split the var_name and the value ...(at least I think...) Commented Jul 27, 2012 at 18:47

2 Answers 2

7

Depending upon how you want to go at it, CosmicComputer is right to refer you to Regular Expressions. If your syntax is this simple, you could always do something like:

line = 'Found mode 1 of 12: EV= 1.5185449E+04, f= 19.612545, T= 0.050988.'

splitByComma=line.split(',')

fValue = splitByComma[1].replace('f= ', '').strip()
print(fValue)

Results in 19.612545 being printed (still a string though).

Split your line by commas, grab the 2nd chunk, and break out the f value. Error checking and conversions left up to you!

Sign up to request clarification or add additional context in comments.

2 Comments

Thats fine that I get the value as a string, as long as I get that value.
Much cleaner then mine. I was just trying to prove a quick point.
0

Using regular expressions here is maddness. Just use string.find as follows: (where string is the name of the variable the holds your string)

index = string.find('f=')
index = index + 2 //skip over = and space 
string = string[index:] //cuts things that you don't need 
string = string.split(',') //splits the remaining string delimited by comma
your_value = string[0] //extracts the first field

I know its ugly, but its nothing compared with RE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.