partial string formatting

前端 未结 21 1029
野的像风
野的像风 2020-11-28 04:30

Is it possible to do partial string formatting with the advanced string formatting methods, similar to the string template safe_substitute() function?

F

21条回答
  •  清酒与你
    2020-11-28 05:03

    If you define your own Formatter which overrides the get_value method, you could use that to map undefined field names to whatever you wanted:

    http://docs.python.org/library/string.html#string.Formatter.get_value

    For instance, you could map bar to "{bar}" if bar isn't in the kwargs.

    However, that requires using the format() method of your Formatter object, not the string's format() method.

提交回复
热议问题