Get dictionary-keys used to format a string

Michael Ströder michael.stroeder at inka.de
Thu Mar 2 16:10:38 EST 2000


Johannes Stezenbach wrote:
> 
> Michael Ströder <michael.stroeder at inka.de> wrote:
> 
> >'%(language)s has %(count)03d quote types.' % vars()
> >
> >I would like to extract the keys in the formatting string to a list.
>
> Try something like this:
> 
> ------------
> class GrabKeys:
>     def __init__(self):
>         self.keys = []
>     def __getitem__(self, name):
>         self.keys.append(name)
>         return 0 # 0 is apparently compatible with all % format 
> 
> gk = GrabKeys()
> 
> '%(language)s has %(count)03d quote types.' % gk
> 
> print gk.keys
> ------------

I like this approach since it seems to be the most complete
solution. With using re there's always some special case which isn't
covered by the regexp in the first and second try...

Thanks to all who replied.

Ciao, Michael.



More information about the Python-list mailing list