Entry
How can I extract all the keys in a formatting string to a list?
How can I extract all the keys in a formatting string to a list?
Mar 3rd, 2000 20:06
Nathan Wallace, unknown unknown, Michael Ströder, Johannes Stezenbach
For example, go from:
'%(language)s has %(count)03d quote types.' % vars()
to get:
['language','count']
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
------------