Regular expression help

Brad brad at 16systems.com
Fri Jul 18 10:12:34 EDT 2008


nclbndk759 at googlemail.com wrote:
> 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...

Why not just split them out instead of using REs?

fp = open("test.txt")
lines = fp.readlines()
fp.close()

for line in lines:
     split = line.split()
     for pair in split:
         pair_split = pair.split("=")
         if len(pair_split) == 2:
             try:
                 print pair_split[0], "is", pair_split[1]
             except:
                 pass

Results:

IDLE 1.2.2      ==== No Subprocess ====
 >>>
afrac is .7
mmom is 0
sev is -9.56646
erep is 0
etot is -11.020107
emad is -3.597647
3pv is 0
 >>>



More information about the Python-list mailing list