Python re.cipe

Kurt Schwer says:

Perl programmers are assumed to know regular expressions. I think python programmers should get more exposure to regular expressions.

So, this morning I needed to process a KML document, adding IDs derived from a name element to all parent Placemark elements. Python's regular expressions aren't as expressive or as built in as Perl's, but there's nothing you can't do by using a callable object as the second argument to re.sub. To convert this:


to this:

<Placemark id="b5">

I just executed the following statements at the interpreter prompt:

>>> kmlin = open('temp.kml').read()
>>> def sub_id(m):
...     g = m.groups()
...     return '<Placemark id="%s">%s<name>%s' % (g[1].lower(), g[0], g[1])
>>> import re
>>> kmlout = re.sub(r'<Placemark>(\s+)<name>(\w\d)', sub_id, kmlin)
>>> f = open('grid.kml', 'w')
>>> f.write(kmlout)
>>> f.close()