Trivial string substitution/parser
Josiah Carlson
josiah.carlson at sbcglobal.net
Mon Jun 18 00:18:03 EDT 2007
Samuel wrote:
> On Sun, 17 Jun 2007 11:00:58 +0000, Duncan Booth wrote:
>
>> The elegant and lazy way would be to change your specification so that $
>> characters are escaped by $$ not by backslashes. Then you can write:
>>
>>>>> from string import Template
>>>>> ...
>
> Thanks, however, turns out my specification of the problem was
> incomplete: In addition, the variable names are not known at compilation
> time.
You mean at edit-time.
>>> t.substitute(variable1="hello", variable2="world")
Can be replaced by...
>>> t.substitute(**vars)
...as per the standard **kwargs passing semantics.
- Josiah
More information about the Python-list
mailing list