I am trying to get an email template together. The message content will be dependent on values within a dictionary. However, the dictionary might not contain all the keys each time.
This currently is fine as all values are in the dictionary ('Title'
, 'Surname'
, 'Additional Details'
):
practise_dict = {"Additional Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}
msg = """From: John Smith <[email protected]>
To: {Title} {Surname} <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
{Additional Details}
<b>This is HTML message.</b>
<h1>This is headline.</h1>
`""".format(**practise_dict)
print(msg)
In the msg
variable I am trying to create my 'template'. This means that I need to have all possible items that could be in the dictionary.
For example the next piece would fail as it is looking for 'Date'
that doesn't exist in this dictionary:
practise_dict = {"Additional Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}
msg = """From: John Smith <[email protected]>
To: {Title} {Surname} <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
{Additional Details}
{Date}
<b>This is HTML message.</b>
<h1>This is headline.</h1>
`""".format(**practise_dict)
print(msg)
Is there a way to ask it to ignore a string substitution if it doesn't exist as a key in the look-up dictionary?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…