<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> I am new to Python, with a background in scientific computing. I'm
> trying to write a script that will take a file with lines like
>
> c afrac=.7 mmom=0 sev=-9.56646 erep=0 etot=-11.020107 emad=-3.597647
> 3pv=0
>
> extract the values of afrac and etot and plot them.
...

> What is being stored in energy is '<_sre.SRE_Match object at
> 0x2a955e4ed0>', not '-11.020107'. Why?

because the re.match() method returns a match object, as documented at 
http://www.python.org/doc/current/lib/match-objects.html

But this looks like a problem where regular expressions are overkill. 
Assuming all your lines are formatted as in the example above (every value 
you are interested in contains an equals sign and is surrounded by spaces), 
you could do this:

values = {}
for expression in line.split(" "):
    if "=" in expression:
        name, val = expression.split("=")
        values[name] = val

I'd wager that this will run a fair bit faster than any regex-based 
solution.  Then you just use values['afrac'] and values['etot'] when you 
need them.

And when you get to be a really hard-core Pythonista, you could write the 
whole routine above in one line, but this seems clearer.  ;-)

Russ



--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to