By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Expand|Select|Wrap|Line Numbers
  1. #read orig file into buf & close file
  2. import re
  3. infile = file('pyInfile', 'r')
  4. buf = infile.read()
  5. infile.close()
  6. print buf
  7.  
  8. #replace all allocated streams with 'rng'
  9. print '=========== orig buf ============'; print buf
  10. pat = re.compile('r\[\d+\]')
  11. buf = pat.sub("rng", buf, 0)
  12.  
  13. #now replace all 'rng's with consecutive streams
  14. #===============================================
  15. def static_num():
  16. ''' this is a generator function that avoids globals
  17. yield differentiates fn as generator fn which freezes
  18. '''
  19. x = 0
  20. while True:
  21. x += 1
  22. yield str(x)
  23.  
  24. static = static_num().next
  25.  
  26. pat = re.compile('rng')
  27. #there is a problem in that static only seems to get called once.
  28. #need to invoke this every time you get a match for it to
  29. #increment
  30. buf = pat.subn('rng[' + static() + ']', buf, 0)
  31. print 'static() incrementing ok ' + static()
  32. print 'static() incrementing ok ' + static()
  33. print '=========== changed to ============'; print buf[0]
  34. #outfile = file('pyOutfile', 'w')
  35. #outfile.write(buf)
  36.  

Oct 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
m g william wrote:
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
Oct 4 '06 #2

P: n/a
"m g william" <mg*******@blueyonder.co.ukwrote in message
news:ma***************************************@pyt hon.org...
<snip>
#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
Oct 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.