python - Ignore str.format(**foo) if key doesn't exist in foo -


I am trying to merge the email template together. The message content will depend on values ​​within a dictionary, however, the dictionary may not have all keys at all times.

This is absolutely right because all values ​​are in dictionary ( 'Title' , 'alias' / "> additional details"):

 < Code> practice_dict = {"additional details": "blah blah blah", "title": "mister", "surname": "smith", "url": "/ test / tester"} msg = "": : John Smith & lt; no -reply@somethingsomething.co.uk> To: {title} {alias} & lt; Blah@blahblah.co.uk> MIME-version: 1.0 Content-type: text / html theme : New Website Inquiry This is an e-mail message to be sent in HTML format { Oblique description}  This is HTML message. & Lt; / b & gt; & lt; h1 & gt; This is the title. & Lt; / h1 & gt; `" ".format (** I am trying to make my template 'print' in print (msg)  

this means that I need all those possible items Which can be in the dictionary.

For example, the next pieces will be unsuccessful as it is in search of 'date' in this dictionary:

  practise_dict = {"Additional Details": "B "Smith", "URL": "/ test / tester"} msg = "": From: John Smith & lt; No -reply@somethingsomething.co.uk> To: {title} {aliases} & lt; Blah@blahblah.co.uk> MIME-version: 1.0 Content-Type: Text / html Subject: New Website Check This is an email message sent in HTML format (additional details) {date} & lt; B & gt; This is HTML message. & Lt; / B & gt; & Lt; H1 & gt; This is the title. & Lt; / H1> `'' .format (** practise_dict) print (msg)  

Is there a way to tell it to ignore a string replacement exists as a key in the look up dictionary? is not?

You can use the template and safe_substitute

  String import from template practice practice_dict = {"Additional_Details": "blah blah blah", "title": "mister", "surname": "smith" "URL": "/ test / tester"} msg = "": From: John Smith & lt; No -reply@somethingsomething.co.uk> To: $ headline $ nickname & lt; Blah@blahblah.co.uk> MIME-Version: 1.0 Content-Type: Text / HTML Subject: New Website Inquiry This is an e-mail message to be sent in HTML format $ extra_ Details $ date & lt; B & gt; This is HTML message. & Lt; / B & gt; & Lt; H1 & gt; This is the title. & Lt; / H1>     From: John Smith & lt; no -reply@somethingsomething.co.uk> To: Mr. Smith & lt; blah@blahblah.co.uk> MIME-version: 1.0 Content-Type: Text / html Subject: New Website Check This is a The e-mail message can be sent in HTML format Blah blah blah $ date & lt; b & gt; this is HTML message. & Lt; / b & gt; & lt; h1 & gt; This is the title. & Lt; / h1 & gt;  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -