How retrieve keyword arguments from a string in python?

前端 未结 2 1904
温柔的废话
温柔的废话 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:40

    Why reinvent the wheel? string.Formatter has the parse() function.

    >>> import string
    >>> [a[1] for a in string.Formatter().parse('{foo}/{bar}')]
    ['foo', 'bar']
    

提交回复
热议问题