Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- class MyForm(string.Formatter):
- def parse(self, format_string):
- for tpl in string.Formatter.parse(self, format_string):
- print(tpl)
- literal_text, field_name, format_spec, conversion = tpl
- if field_name:
- literal_text += f'"{field_name}"'
- yield literal_text, None, None, None
- else:
- yield tpl
- fmt = '{abc} {def} {3} {4} foo bar not in brackets {hello} {5,7} end'
- print(fmt)
- x = MyForm()
- print(x.format(fmt))
- """
- {abc} {def} {3} {4} foo bar not in brackets {hello} {5,7} end
- ('', 'abc', '', None)
- (' ', 'def', '', None)
- (' ', '3', '', None)
- (' ', '4', '', None)
- (' foo bar not in brackets ', 'hello', '', None)
- (' ', '5,7', '', None)
- (' end', None, None, None)
- "abc" "def" "3" "4" foo bar not in brackets "hello" "5,7" end
- """
Add Comment
Please, Sign In to add comment