Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99158 > unrolled thread

Re: String format - resolve placeholders names

Started byPeter Otten <__peter__@web.de>
First post2015-11-20 16:53 +0100
Last post2015-11-20 20:36 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: String format - resolve placeholders names Peter Otten <__peter__@web.de> - 2015-11-20 16:53 +0100
    Re: String format - resolve placeholders names Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-20 17:08 +0000
      Re: String format - resolve placeholders names Ervin Hegedüs <airween@gmail.com> - 2015-11-20 20:36 +0100

#99158 — Re: String format - resolve placeholders names

FromPeter Otten <__peter__@web.de>
Date2015-11-20 16:53 +0100
SubjectRe: String format - resolve placeholders names
Message-ID<mailman.3.1448034840.2291.python-list@python.org>
Ervin Hegedüs wrote:

> Python has a good string formatter, eg. I can do this:
> 
> s = "{who} likes {what}"
> d = {'who': "Adam", 'what': "ants"}
> s.format(**d)
> 
> result:
> 'Adam likes ants'
> 
> Is it possible, and if yes, how to resolve the placeholders names
> in string?

>>> import string
>>> for item in string.Formatter().parse("{who} likes {what}"):
...     print(item)
... 
('', 'who', '', None)
(' likes ', 'what', '', None)

[toc] | [next] | [standalone]


#99170

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-11-20 17:08 +0000
Message-ID<n2nk25$mns$1@dont-email.me>
In reply to#99158
On Fri, 20 Nov 2015 16:53:47 +0100, Peter Otten wrote:

> Ervin Hegedüs wrote:

>> Python has a good string formatter, eg. I can do this:

>> s = "{who} likes {what}"
>> d = {'who': "Adam", 'what': "ants"}
>> s.format(**d)

>> result:
>> 'Adam likes ants'

>> Is it possible, and if yes, how to resolve the placeholders names in
>> string?

>>>> import string for item in string.Formatter().parse("{who} likes
>>>> {what}"):
> ...     print(item)
> ...
> ('', 'who', '', None)
> (' likes ', 'what', '', None)

Or even:

>>> s = "{who} likes {what}"
>>> d = {'who': "Adam", 'what': "ants"}
>>> keys = [x[1] for x in string.Formatter().parse(s)]
>>> keys
['who', 'what']

then ...

for key in keys:
    if key not in d:
        raise KeyError("Missing key '{}' in format string '{}'".format
(key, s))

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#99185

FromErvin Hegedüs <airween@gmail.com>
Date2015-11-20 20:36 +0100
Message-ID<mailman.20.1448048195.2291.python-list@python.org>
In reply to#99170
Hi Denis,

On Fri, Nov 20, 2015 at 05:08:21PM -0000, Denis McMahon wrote:
> On Fri, 20 Nov 2015 16:53:47 +0100, Peter Otten wrote:
> 
> > Ervin Hegedüs wrote:
> 
> >> Python has a good string formatter, eg. I can do this:
> 
 
> Or even:
> 
> >>> s = "{who} likes {what}"
> >>> d = {'who': "Adam", 'what': "ants"}
> >>> keys = [x[1] for x in string.Formatter().parse(s)]
> >>> keys
> ['who', 'what']
> 
> then ...
> 
> for key in keys:
>     if key not in d:
>         raise KeyError("Missing key '{}' in format string '{}'".format
> (key, s))

well, I think I'm very satisfied - I got better and more better
solutions :).


Thanks for all, folks:

a.
 

-- 
I � UTF-8

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web