replace string patern with different value
Peter Otten
__peter__ at web.de
Mon May 9 09:47:41 EDT 2005
ajikoe at gmail.com wrote:
> I would like to replace string with different values,
> For example :
> source = 'kode1 bla bla kode1 bla kode1'
> I have a list with each member will replace each of kode1.
> L = [11,22,33]
> So the new source will become:
> newsource = '11 bla bla 22 bla 33'
Here's one way to do it with regular expressions:
>>> def make_sub(items):
... items = iter(items)
... def sub(m):
... return items.next()
... return sub
...
>>> re.compile("kode1").sub(make_sub(str(i) for i in [11, 22, 33]),
... "kode1 bla bla kode1 bla kode1")
'11 bla bla 22 bla 33'
Peter
More information about the Python-list
mailing list