The pypi package parse
serves this purpose well:
pip install parse
Can be used like this:
>>> import parse
>>> result=parse.parse('Version {0}.{1}.{2}\n', 'Version 1.15.6\n')
<Result ('1', '15', '6') {}>
>>> values=list(result)
>>> print(values)
['1', '15', '6']
Note that the docs say the parse
package does not EXACTLY emulate the format specification mini-language by default; it also uses some type-indicators specified by re
. Of special note is that s
means "whitespace" by default, rather than str
. This can be easily modified to be consistent with the format specification by changing the default type for s
to str
(using extra_types
):
result = parse.parse(format_str, string, extra_types=dict(s=str))
Here is a conceptual idea for a modification of the string.Formatter
built-in class using the parse
package to add unformat
capability that I have used myself:
import parse
from string import Formatter
class Unformatter(Formatter):
'''A parsable formatter.'''
def unformat(self, format, string, extra_types=dict(s=str), evaluate_result=True):
return parse.parse(format, string, extra_types, evaluate_result)
unformat.__doc__ = parse.Parser.parse.__doc__
IMPORTANT: the method name parse
is already in use by the Formatter
class, so I have chosen unformat
instead to avoid conflicts.
UPDATE: You might use it like this- very similar to the string.Formatter
class.
Formatting (identical to '{:d} {:d}'.format(1, 2)
):
>>> formatter = Unformatter()
>>> s = formatter.format('{:d} {:d}', 1, 2)
>>> s
'1 2'
Unformatting:
>>> result = formatter.unformat('{:d} {:d}', s)
>>> result
<Result (1, 2) {}>
>>> tuple(result)
(1, 2)
This is of course of very limited use as shown above. However, I've put up a pypi package (parmatter - a project originally for my own use but maybe others will find it useful) that explores some ideas of how to put this idea to more useful work. The package relies heavily on the aforementioned parse
package. EDIT: a few years of experience under my belt later, I realized parmatter
(my first package!) was a terrible, embarrassing idea and have since deleted it.