How retrieve keyword arguments from a string in python?

前端 未结 2 1874
温柔的废话
温柔的废话 2021-01-15 04:23

Let\'s say, I have a string:

  > my_string = \'{foo}/{bar}\'
  > my_string.format(foo=\'foo\', bar=\'bar\')
  \'foo/bar\'

Right, coo

2条回答
  •  情话喂你
    2021-01-15 04:47

    You can use the string.Formatter.parse method. It splits the string into its literal text components and fields:

    In [1]: import string
    
    In [2]: formatter = string.Formatter()
    
    In [3]: text = 'Here is some text with {replacement} fields {}'
    
    In [4]: list(formatter.parse(text))
    Out[4]: 
    [('Here is some text with ', 'replacement', '', None),
     (' fields ', '', '', None)]
    

    To retrieve the names fields simply iterate over the result and collect the second field.

    Note that this will include positional (both numbered and unnumbered) arguments as well.

    Note that this does not include nested arguments:

    In [1]: import string
    
    In [2]: formatter = string.Formatter()
    
    In [3]: list(formatter.parse('{hello:{world}}'))
    Out[3]: [('', 'hello', '{world}', None)]
    

    If you want to get all named fields (assuming only named fields are used), you have to parse the second element in the tuple:

    In [4]: def get_named_fields(text):
       ...:     formatter = string.Formatter()
       ...:     elems = formatter.parse(text)
       ...:     for _, field, spec, _ in elems:
       ...:         if field:
       ...:             yield field
       ...:         if spec:
       ...:             yield from get_named_fields(spec)
       ...:             
    
    In [5]: list(get_named_fields('{hello:{world}}'))
    Out[5]: ['hello', 'world']
    

    (This solution would allow arbitrarily deep format specifiers, while only one level would be sufficient).

提交回复
热议问题