Get dictionary-keys used to format a string

Johannes Stezenbach yawyi at gmx.de
Thu Mar 2 13:52:14 EST 2000


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.
>Well I can parse it myself but is there a built-in function to
>extract a list/tuple of the dictionary keys, e.g.
>['language','count'] in this case?

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 characters

gk = GrabKeys()

'%(language)s has %(count)03d quote types.' % gk

print gk.keys
------------

HTH,
Johannes




More information about the Python-list mailing list