I'm creating a class that renames a file using a user-specified format. This format will be a simple string whose str.format
method will be called to fill in the blanks.
It turns out that my procedure will require extracting variable names contained in braces. For example, a string may contain {user}
, which should yield user
. Of course, there will be several sets of braces in a single string, and I'll need to get the contents of each, in the order in which they appear and output them to a list.
Thus, "{foo}{bar}"
should yield ['foo', 'bar']
.
I suspect that the easiest way to do this is to use re.split
, but I know nothing about regular expressions. Can someone help me out?
Thanks in advance!
Another possibility is to use Python's actual Formatter itself to extract the field names for you:
>>> import string
>>> s = "{foo} spam eggs {bar}"
>>> string.Formatter().parse(s)
<formatteriterator object at 0x101d17b98>
>>> list(string.Formatter().parse(s))
[('', 'foo', '', None), (' spam eggs ', 'bar', '', None)]
>>> field_names = [name for text, name, spec, conv in string.Formatter().parse(s)]
>>> field_names
['foo', 'bar']
or (shorter but less informative):
>>> field_names = [v[1] for v in string.Formatter().parse(s)]
>>> field_names
['foo', 'bar']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With