[Tutor] inserting variables into a docstring

Hugo Arts hugo.yoshi at gmail.com
Sun Aug 22 06:27:36 CEST 2010


On Sat, Aug 21, 2010 at 11:10 PM, Bill Allen <wallenpb at gmail.com> wrote:
> I am trying to build an HTML body text for an email using a docstring with
> variables inserted a certain points.  It all works just fine, except that
> the first instance of the variable "pecn" in the HTML link does not get
> inserted into the text.  The second instance of "pecn" gets inserted ok into
> that HTML link.   What is the right way to do this or is there a better way
> in general?
>

The variable is inserted just fine for me, though there's some
problems with the <a> tag, because you shouldn't surround GET
variables with quotation marks, e.g. you should do this:

http://example.com/?ecn=423434
NOT this:
http://example.com/?ecn="423434"

If you do this, the HTML parser will encounter the first ", think that
it's come to the end of the href="" attribute, and ignore the rest.
But that's an HTML issue, not a python one.

As an aside, consider using string formatting operations like so:

newMail.HTMLBody = """
<pre>
ECN number {0} on Project {1} has been closed.
Below is a link to the ECN on the intranet.
<a href="http://web_server_at_work.com/cgi-bin/part-url.cgi?ecn={0}">{0}</a>
ECN Description:
{2}""".format(pecn, pecn_project, pdesc)

Hugo


More information about the Tutor mailing list