Get dictionary-keys used to format a string

Fredrik Lundh effbot at telia.com
Thu Mar 2 18:43:02 CET 2000


Michael Ströder <michael.stroeder at inka.de> wrote:
> Hence the example from the Python docs:
>
> '%(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?


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

>>> import re
>>> re.findall(r'%\((\w+)\)', s)
['language', 'count']

hope this helps!

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list