Count and replacing strings a texfile

Greg Jorgensen gregj at pobox.com
Tue Jan 23 23:34:09 EST 2001


In article <mailman.980226386.12769.python-list at python.org>,
  Moshe Zadka <moshez at zadka.site.co.il> wrote:
> On Mon, 22 Jan 2001 21:42:21 GMT, puckoh at my-deja.com wrote:
>
> counter = 0
> def replace(_):
> 	global counter
> 	counter += 1
> 	return counter
>
> id_re = re.compile('%id%')
>
> s = open(file).read()
> s = id_re.replace(s, replace)

I think you mean re.sub(replace, s)... replace() is a similar method
for string objects, but it doesn't accept a function for the
replacement string.

--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list