Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99157
| From | Ervin Hegedüs <airween@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: String format - resolve placeholders names |
| Date | 2015-11-20 16:53 +0100 |
| Message-ID | <mailman.2.1448034770.2291.python-list@python.org> (permalink) |
| References | <20151120145227.GB13994@arxnet.hu> <CAPTjJmoBY=tN3-_AnyrC90QJY6J8fWv6uuC=ORwBknvfi8FYWw@mail.gmail.com> |
Hello Chris,
On Sat, Nov 21, 2015 at 02:06:11AM +1100, Chris Angelico wrote:
> On Sat, Nov 21, 2015 at 1:52 AM, Ervin Hegedüs <airween@gmail.com> wrote:
> > Python has a good string formatter, eg. I can do this:
> >
> > s = "{who} likes {what}"
> > d = {'who': "Adam", 'what': "ants"}
> > s.format(**d)
...
> > But is there any other (direct) way, which keywords exists in
> > string?
>
> I think what you're asking for can be done using format_map with a
> custom mapping object:
>
> >>> class IdentiMap:
> ... def __init__(self):
> ... self.keys = []
> ... def __getitem__(self, key):
> ... self.keys.append(key)
> ...
> >>> m = IdentiMap()
> >>> "{who} likes {what}".format_map(m)
> 'None likes None'
> >>> m.keys
> ['who', 'what']
>
> Does that help?
absolutely, many thanks again!
a.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: String format - resolve placeholders names Ervin Hegedüs <airween@gmail.com> - 2015-11-20 16:53 +0100
csiph-web