Replace various regex

Martin mdekauwe at gmail.com
Fri Feb 12 20:39:18 CET 2010


Hi,

I am trying to come up with a more generic scheme to match and replace
a series of regex, which look something like this...

19.01,16.38,0.79,1.26,1.00   !  canht_ft(1:npft)
5.0, 4.0, 2.0, 4.0, 1.0      !  lai(1:npft)

Ideally match the pattern to the right of the "!" sign (e.g. lai), I
would then like to be able to replace one or all of the corresponding
numbers on the line. So far I have a rather unsatisfactory solution,
any suggestions would be appreciated...

The file read in is an ascii file.

f = open(fname, 'r')
s = f.read()

if CANHT:
    s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+   !
canht_ft", CANHT, s)

where CANHT might be

CANHT = '115.01,16.38,0.79,1.26,1.00   !  canht_ft'

But this involves me passing the entire string.

Thanks.

Martin



More information about the Python-list mailing list