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


Groups > comp.lang.python > #63286

Re: word replacing in a paragraph

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'automate': 0.07; 'literal': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'template': 0.14; 'cui': 0.16; 'data)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'seconds.': 0.16; 'str.format()': 0.16; 'substituting': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'value.': 0.19; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'replace': 0.24; 'skip:% 10': 0.24; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'gives': 0.31; 'values.': 0.31; 'probably': 0.32; 'text': 0.33; 'test': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'example,': 0.37; 'easiest': 0.38; 'initially': 0.38; 'how': 0.40; 'simply': 0.61; 'today': 0.64; 'frank': 0.68; 'flexibility,': 0.84; 'to:none': 0.92; 'yourself,': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=GdHy8Ju0Q4/At2qatWDiXT78H6MgDeN4MBiXyHnZoIE=; b=hNgsj7zkmhKTydlo++6MiLMrJ9qGYwxv8pFhMko7wEnboCqDCINSNzNjXoue9nEhmf 8Lie+ke8HgnfyAD9HrCUT4YooziP6oJgtZJkTLE+7Qgyd2XBltbtkvNVv1msbdUviI9y RHIii8HCJlecxzSI1JzY4cBmNpuoOV+ettjRzcgGo2e2xx+qXiHtoOHU3pVKb4s60Z2/ thcBwTbs3LqNeIY1uxi0v+/Xm05G5txuYh0SOxNn88p2SeBzEHx3X4h91OmcMFfkXXOi 7l2YiMlHL412cqssl8C5VLKM+AJ3cwWaEGoZ/xr/LOcEXItZxiWrcsjQ+F3z47eDKQch bKEQ==
MIME-Version 1.0
X-Received by 10.68.108.194 with SMTP id hm2mr128628211pbb.22.1389021396147; Mon, 06 Jan 2014 07:16:36 -0800 (PST)
In-Reply-To <BAY176-W84F6BD350557828CDAD2DDAB70@phx.gbl>
References <BAY176-W84F6BD350557828CDAD2DDAB70@phx.gbl>
Date Tue, 7 Jan 2014 02:16:36 +1100
Subject Re: word replacing in a paragraph
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.5021.1389021399.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389021399 news.xs4all.nl 2858 [2001:888:2000:d::a6]:37432
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63286

Show key headers only | View raw


On Tue, Jan 7, 2014 at 1:01 AM, Frank Cui <ycui@outlook.com> wrote:
> I'm trying to automate a process by initially creating a standard template
> and then replace some text fields with variable values.
>
> [for example, "DATE" in the paragraph will be replaced by the current date
> value. it doesn't have to be a literal word of "DATE", "DATE" in "TESTDATE"
> can also be replaced.]

How much do you trust the person creating that template? If it's you
yourself, then probably the easiest way is to use the inbuilt
str.format() method or %-formatting:

template = """Today is: %(date)s
Test ran on %(version)s, in %(time)f seconds."""

data = {"date":"2014-01-07", "version":"v3.4.0b2", "time":123.45}

print(template % data)

Today is: 2014-01-07
Test ran on v3.4.0b2, in 123.450000 seconds.

This gives you a lot of flexibility, as you can lay things out tidily
as well as simply substituting in values.

ChrisA

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


Thread

Re: word replacing in a paragraph Chris Angelico <rosuav@gmail.com> - 2014-01-07 02:16 +1100

csiph-web