Search code examples
python-2.7text-extractionstring.format

Reversing str.format() - getting arguments by placeholder


What is the most elegant way to reverse the process of Python's str.format on replacing arguments by position?

For example for the following

textpattern = 'Please move {0} to {1}'
mystring = 'Please move a to b'

a deformat function :

mystring.deformat(textpattern)

will return the list ['a', 'b']. The variable textpattern is being entered by the user and then dynamically fetched from the DB.

The closest thing I found is a question about scnaf in Python but that's slightly different.


Solution

  • You're looking for the package parse which is described as: parse() is the opposite of format().