Extract keys from Python's format strings

Sometimes in Python you may need to diagnose some format strings. Maybe they are supposed to be provided by the user and you need to know which keys to fill it with. Another prominent example is translation software: format string placeholders are definitely a thing there, and you may want to do something special about them.

First, let's get the old-style format strings out of the way. I haven't seen any introspection capabilities for them in the standard library, so instead this neat workaround can be used: a format string is formatted by a dict-like object, which really just stores every key that it gets asked for.


wzxhzdk:0

If a format string with positional arguments is supplied here, TypeError will be raised by the formatting operator.


For new-style format strings the exact same class can be used, together with str.format_map in Python 3.2+.

But there is a much better way: string.Formatter.parse:

wzxhzdk:1

Format spec is ignored here, but feel free to extract it as well.

Created (last updated )
Comments powered by Disqus