extracting string.Template substitution placeholders

Eric S. Johansson esj at harvee.org
Tue Jan 14 22:07:55 EST 2014


On 1/13/2014 2:24 AM, Steven D'Aprano wrote:
> On Sun, 12 Jan 2014 10:08:31 -0500, Eric S. Johansson wrote:
>
>
> Now just walk the template for $ signs. Watch out for $$ which escapes
> the dollar sign. Here's a baby parser:
found a different way

import string
cmplxstr="""a simple $string a longer $string a $last line"""
nst=string.Template(cmplxstr)

identifiers = {}

while True:
     try:
         result = nst.substitute(identifiers)
     except KeyError, error:
         print error
         identifiers[error[0]] = "x"
     else:
         break
print "loop done"

  ------
at the end I only care about the keys in identifier which I fill in 
after user interaction.




More information about the Python-list mailing list