5

I want to parse a string to extract all the substrings in curly braces:

'The value of x is {x}, and the list is {y} of len {}'

should produce:

(x, y)

Then I want to format the string to print the initial string with the values:

str.format('The value of x is {x}, and the list is {y} of len {}', x, y, len(y))

How can I do that?

Example usage:
def somefunc():
    x = 123
    y = ['a', 'b']
    MyFormat('The value of x is {x}, and the list is {y} of len {}',len(y))

output:
    The value of x is 123, and the list is ['a', 'b'] of len 2
3
  • 1
    So after extracting x and y, do you plan to modify it? Before printing it again Commented Jun 18, 2015 at 11:25
  • 1
    What's the point of the intermediate step? It doesn't help you with the next part at all! Commented Jun 18, 2015 at 11:27
  • 1
    Your question is not clear!! can you show us a Minimal, Complete, and Verifiable example about what you want to do?or the code that you have tried so far? Commented Jun 18, 2015 at 11:31

3 Answers 3

6

You can use string.Formatter.parse:

Loop over the format_string and return an iterable of tuples (literal_text, field_name, format_spec, conversion). This is used by vformat() to break the string into either literal text, or replacement fields.

The values in the tuple conceptually represent a span of literal text followed by a single replacement field. If there is no literal text (which can happen if two replacement fields occur consecutively), then literal_text will be a zero-length string. If there is no replacement field, then the values of field_name, format_spec and conversion will be None.

from string import Formatter

s = 'The value of x is {x}, and the list is {y} of len {}'

print([t[1] for t in Formatter().parse(s) if t[1]])
['x', 'y']

Not sure how that really helps what you are trying to do as you can just pass x and y to str.format in your function or use **locals:

def somefunc():
    x = 123
    y = ['a', 'b']
    print('The value of x is {x}, and the list is {y} of len {}'.format(len(y),**locals()))

If you wanted to print the named args you could add the Formatter output:

def somefunc():
    x = 123
    y = ['a', 'b']
    print("The named args are {}".format( [t[1] for t in Formatter().parse(s) if t[1]]))
    print('The value of x is {x}, and the list is {y} of len {}'.format(len(y), **locals()))

Which would output:

The named args are ['x', 'y']
The value of x is 123, and the list is ['a', 'b'] of len 2
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your solution like this:_print (s.format(**locals()))_, and got the following error IndexError: tuple index out of range
0

You may use re.findall

>>> import re
>>> s = 'The value of x is {x}, and the list is {y} of len {}'
>>> re.findall(r'\{([^{}]+)\}', s)
['x', 'y']
>>> tuple(re.findall(r'\{([^{}]+)\}', s))
('x', 'y')

Comments

0

What are you doing after you extract the value?

import re
st = "The value of x is {x}, and the list is {y} of len {}"
exp = re.compile(r"\{(.+?)\}")

print(tuple(exp.findall(st)))

Output is

 ('x', 'y')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.