[Tutor] generating formatted output

Bill Allen wallenpb at gmail.com
Mon Sep 27 00:30:20 CEST 2010


On Sun, Sep 26, 2010 at 4:21 PM, Hugo Arts <hugo.yoshi at gmail.com> wrote:

> On Sun, Sep 26, 2010 at 10:16 PM, Rance Hall <ranceh at gmail.com> wrote:
> > My app will be printing a series of documents that are the same each
> > time the doc is printed with the exception of the variables.  Sort of
> > a MailMerge if you will.
> >
> >
>
> I would suggest you take a look at string.Template or the str.format
> method. It may be somewhat simpler than doing a whole lot of replaces,
> perhaps faster as well.
>
> http://docs.python.org/library/string.html#template-strings
> http://docs.python.org/library/stdtypes.html#str.format
>
> Hugo
>
> I agree.   I had a great deal of success with using docstrings along with
str.format in a program I wrote at work.

Example, two ways:

"""
Dear Mr. {0},

Thanks for your recent inquiry.

Your current invoice #{1} is overdue by {2} days.

Please make remittance by {3} in order to avoid
overdue fees.

Sincerely,
{4}
Billing Dept.
""".format(client_name, inv_num, num_days, due_date, sender)

or

over_due_notice = """

Dear Mr. {0},

Thanks for your recent inquiry.

Your current invoice #{1} is overdue by {2} days.

Please make remittance by {3} in order to avoid
overdue fees.

Sincerely,
{4}
Billing Dept.
"""

over_due_notice.format(client_name, inv_num, num_days, due_date, sender)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100926/96e802d3/attachment.html>


More information about the Tutor mailing list