parsing the output from matlab

wang frank fw3 at
Thu Oct 25 20:38:28 CEST 2007

Hi, Travis,
Thanks very much for your help. Since each day, my mail box is flooded with python forum email. I simply overlooked your email, eventhough I am desperately waiting for the help. Today when I googled the topic and found your reply.
I am sorry that I send a similar help request to the forum today.

Date: Tue, 23 Oct 2007 10:08:28 -0400From: travis.brady at gmail.comTo: python-list at python.orgSubject: Re: parsing the output from matlab
On 10/22/07, wang frank <fw3 at> wrote 

 I have a big log file generated from matlabe, for each variable, it print the name of the variable and an empty line and then the value. such as: x1 =     0.1 y =    7 z =     6.7 x1 =    0.5 I want to use python to parse the file and selectively print out the vairable and its value. For example, I want to print out all the value related with x1, so the output will be x1 = 0.1x1 = 0.5. 
Here is a fairly naive version with re named groups that should handle the example you pasted. 

In [62]: import re

In [63]: px = re.compile('(?P<variable>\w+)\s=\s+(?P<value>\d.*\d*)')

In [64]: for var in px.finditer(s):
    print "%s = %s" %('variable'),'value'))
a = 0.1
y = 7
z = 6.7
x1 = 0.5

To filter for only the x1's just test for the group named 'variable':

In [66]: for var in px.finditer(s):
   ....:     if'variable')=='x1':
   ....:         print "%s = %s" %('variable'),'value')) 
x1 = 0.5

But I'm betting these files get more complex than just the snippet you included, in which case it's probably worth looking at pyparsing and regular expressions.

-- Travis Brady 
広告表示なし!アカウント有効期限なし!Hotmail Plus のお申し込みはこちら
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list