faqts : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 12 people (83%) answered Yes
Recently 2 of 3 people (67%) answered Yes

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
------------