replacing all 'rng's in a buffer with consecutive r[1], r[2]'s

M

m g william

I read a file into a buffer and subject it to re.sub()
I can replace every occurrence of a pattern with a fixed string but when
I try to replace each occurrence with a string that changes (by having
an incrementing number in it, (ie 'repTxt[1]','repTxt[2]'etc), I note
that the incrementing number generator function, I'm calling in
re.sub(), (which works fine outside it), seems to be evaluated only once
and is therefore not incrementing the number.

Can someone please show me a working eg of how to replace 'rng' in a
file with 'r[1]', 'r[2]' etc. This is my first Python program so any
help would be very gratefully received.

Here's my code

Code:
#read orig file into buf & close file
import re
infile = file('pyInfile', 'r')
buf = infile.read()
infile.close()
print buf

#replace all allocated streams with 'rng'
print '=========== orig buf ============'; print buf
pat = re.compile('r\[\d+\]')
buf = pat.sub("rng", buf, 0)

#now replace all 'rng's with consecutive streams
#===============================================
def static_num():
    ''' this is a generator function that avoids globals
yield differentiates fn as generator fn which freezes
'''
    x = 0
    while True:
       x += 1
       yield str(x)

static = static_num().next

pat = re.compile('rng')
#there is a problem in that static only seems to get called once.
#need to invoke this every time you get a match for it to
#increment
buf = pat.subn('rng[' + static() + ']', buf, 0)
print 'static() incrementing ok ' + static()
print 'static() incrementing ok ' + static()
print '=========== changed to ============'; print buf[0] 
#outfile = file('pyOutfile', 'w')
#outfile.write(buf)
 
P

Peter Otten

m said:
I read a file into a buffer and subject it to re.sub()
I can replace every occurrence of a pattern with a fixed string but when
I try to replace each occurrence with a string that changes (by having
an incrementing number in it, (ie 'repTxt[1]','repTxt[2]'etc), I note
that the incrementing number generator function, I'm calling in
re.sub(), (which works fine outside it), seems to be evaluated only once
and is therefore not incrementing the number.

Can someone please show me a working eg of how to replace 'rng' in a
file with 'r[1]', 'r[2]' etc. This is my first Python program so any
help would be very gratefully received.

buf = pat.subn('rng[' + static() + ']', buf, 0)

You'll have to repeat that to get the desired effect:

pat = re.compile("rng")
replacements = 1
while replacements:
buf, replacements = pat.subn("r[" + static() + "]", buf, 1)
print buf

but there is a more efficient alternative

def fsub(match):
return "r[" + static() + "]"
buf = re.sub("rng", fsub, buf)

that would still work if you were to replace 'rng' with 'rng[1]',
'rng[2]'...

Peter
 
P

Paul McGuire

#now replace all 'rng's with consecutive streams
#===============================================
def static_num():
''' this is a generator function that avoids globals
yield differentiates fn as generator fn which freezes
'''
x = 0
while True:
x += 1
yield str(x)

static = static_num().next

Also, check out itertools.count (one of many tools from the excellent
itertools module), as in:

static - itertools.count().next

- no need to roll this function for yourself. You still need to call it for
each substitution, though, as described elsewhere.

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top