[Tutor] string.Template
Albert-Jan Roskam
fomcl at yahoo.com
Tue Apr 23 16:14:06 CEST 2013
Hello,
Is there a better, *built-in* alternative for the code below? The recursion works,
but it feels like reinventing the wheel.
import string
def translate(template, *args):
"""Recursively $-substitute <template> using <args> as a replacement"""
syntax = string.Template(template).substitute(*args)
if "$" in syntax:
return translate(syntax, *args)
return syntax
template = """\
Monty $surname
$more
"""
more = """\
The quest for the holy $grail
"""
surname="Python"
grail="Grail"
print translate(template, locals())
Thank you!
Regards,
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the Tutor
mailing list