Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63286
| References | <BAY176-W84F6BD350557828CDAD2DDAB70@phx.gbl> |
|---|---|
| Date | 2014-01-07 02:16 +1100 |
| Subject | Re: word replacing in a paragraph |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5021.1389021399.18130.python-list@python.org> (permalink) |
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
Re: word replacing in a paragraph Chris Angelico <rosuav@gmail.com> - 2014-01-07 02:16 +1100
csiph-web