Determining a replacement dictionary from scratch

Dan moskow23 at
Sun Jan 18 20:16:31 CET 2004


I'd like to be able to take a formatted string and determine the
replacement dictionary necessary to do string interpolation with it. 
For example:

>>> str = 'his name was %(name)s and i saw him %(years)s ago.'
>>> createdict( str )
{'name':'', 'years':''}

Notice how it would automatically fill in default values based on
type.  I figure since python does this automatically maybe there is a
clever way to solve the problem.  Otherwise I will just have to parse
the string myself.  Any clever solutions to this?


More information about the Python-list mailing list