Regular expression help

nclbndk759 at googlemail.com nclbndk759 at googlemail.com
Fri Jul 18 09:43:03 EDT 2008


Hello,

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. I'm really
struggling with getting the values of efrac and etot. So far I have
come up with (small snippet of script just to get the energy, etot):

def get_data_points(filename):
    file = open(filename,'r')
    data_points = []
    while 1:
        line = file.readline()
        if not line: break
        energy = get_total_energy(line)
        data_points.append(energy)
    return data_points

def get_total_energy(line):
    rawstr = r"""(?P<key>.*?)=(?P<value>.*?)\s"""
    p = re.compile(rawstr)
    return p.match(line,5)

What is being stored in energy is '<_sre.SRE_Match object at
0x2a955e4ed0>', not '-11.020107'. Why? I've been struggling with
regular expressions for two days now, with no luck. Could someone
please put me out of my misery and give me a clue as to what's going
on? Apologies if it's blindingly obvious or if this question has been
asked and answered before.

Thanks,

Nicole



More information about the Python-list mailing list