[Tutor] Working with output of a subprocess

Alan Gauld alan.gauld at btinternet.com
Sat Jan 12 02:07:31 CET 2013


On 11/01/13 22:29, 3n2 Solutions wrote:

> Need some help with working with a text file.
> Is it possible to get the values associated with each of the parameter
> in the below text file format? For example:

Yes, but it's non trivial. You need to parse the text.

> 1. How can I check what the Min and Max values are?
> 2. How to check the second column value associated with "epoch2"?

In both cases you can extract the line using the startswith() string 
method. You can then use a combination of  split() and strip(), lstrip() 
and rstrip() and slicing to extract the substrings you need.
Then convert the strings to numbers as necessary.

I'd probably write value extraction functions for each required 
parameter, given a line of input. Then iterate over the text and all the 
appropriate function based on the startswith() result:

for line in open('myfile.txt'):
     if line.startswith('Min'): min = getSingleNumber(line)
     elif line startswith('Max'): max = getSingleNumber(line)
     elif ....
     elif line.startswith('epoch2'): epoch2 = getCol2(line)
     ....


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list