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


Groups > comp.lang.python > #99184

Re: String format - resolve placeholders names

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ervin Hegedüs <airween@gmail.com>
Newsgroups comp.lang.python
Subject Re: String format - resolve placeholders names
Date Fri, 20 Nov 2015 20:34:10 +0100
Lines 32
Message-ID <mailman.19.1448048061.2291.python-list@python.org> (permalink)
References <20151120145227.GB13994@arxnet.hu> <n2nfme$hfn$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de Hrnos9i3o04JY0N7ULaCbwYVq1olsg5quStZzPzh1TCg==
Return-Path <airween@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'',": 0.07; 'none)': 0.07; 'utf-8': 0.07; 'cc:addr:python-list': 0.09; 'python': 0.10; "('',": 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'subject:String': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cc:no real name:2**0': 0.22; 'this:': 0.23; 'thanks,': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'fri,': 0.27; 'solution,': 0.29; 'skip:s 30': 0.31; 'received:google.com': 0.35; 'nov': 0.35; 'possible,': 0.35; 'received:74.125.82': 0.35; 'item': 0.35; 'subject:: ': 0.37; 'names': 0.38; '20,': 0.66; 'otten': 0.84; 'peter,': 0.84; 'received:hu': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=MbjtSJw50ITdnn33aEW4lgtdfoYDt75WIXgqedmMYHc=; b=l4B0xdhEufS8MDFbice1IDUDMkiue/qFGD25WskSaN/S7M8r6iGiYLU+s6ob0Kirqn 0fym0MKyyVTyQKaGGyI2ArCzYUnUn8PGICqlAmjXKChxcZrl/D0oX5opWUndN6OIDh3o 0l9G/+UZK4eVjQ6IbJJkt1hlEsW2whGnl9BpWwxGyFDh1329+jXC9A43wE0vjjI8npPQ i1D3xdK5qGj9HqjotUFMd3lRKEeRqynUzNyyIjqzV+uIv4sbjPUn2NXfayyXz5vdtr0F /X4yPICpa6MhIrtERE6tP+zrTa6vCdqJIZei5dvmm1HppPjkAV3ByOBrKINy1MJTrrVV bjDQ==
X-Received by 10.194.120.194 with SMTP id le2mr16191220wjb.74.1448048053269; Fri, 20 Nov 2015 11:34:13 -0800 (PST)
Content-Disposition inline
In-Reply-To <n2nfme$hfn$1@ger.gmane.org>
User-Agent Mutt/1.5.21 (2010-09-15)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99184

Show key headers only | View raw


Hi Peter,

On Fri, Nov 20, 2015 at 04:53:47PM +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)

nice solution, thanks,


a.
 

-- 
I � UTF-8

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: String format - resolve placeholders names Ervin Hegedüs <airween@gmail.com> - 2015-11-20 20:34 +0100

csiph-web