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

2019-02-24 10:35发布

问题:

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 <no-reply@somethingsomething.co.uk>
To: {Title} {Surname} <blah@blahblah.co.uk>
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 <no-reply@somethingsomething.co.uk>
To: {Title} {Surname} <blah@blahblah.co.uk>
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?

回答1:

You can use Template and safe_substitute for that:

from string import Template

practise_dict = {"Additional_Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}

msg = """From: John Smith <no-reply@somethingsomething.co.uk>
To: $Title $Surname <blah@blahblah.co.uk>
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>
`"""

s = Template(msg).safe_substitute(**practise_dict)
print(s)

OUTPUT

From: John Smith <no-reply@somethingsomething.co.uk>
To: Mr Smith <blah@blahblah.co.uk>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
blah blah blah blah
$Date
<b>This is HTML message.</b>
<h1>This is headline.</h1>


回答2:

I don't think there's a way to tell format that some substitutions are optional. However, here are two workarounds.

If there's only one missing field, try something like:

msg = """...""".format(Date=practise_dict.pop("Date", ""), # handle optional field
                       **practise_dict) # pass the rest as before

However, note that this modifies practise_dict, and will be awkward if there are several values to deal with.

If there may be multiple fields missing, or you don't want to modify the dictionary, the following would probably be preferable:

defaults = {"Date": "", "Additional Details": ""} # define optional fields
defaults.update(practise_dict) # override user data where available
msg = """...""".format(**defaults)

Both approaches will still fail for keys you haven't explicitly provided an alternative for, which allows you to make some fields mandatory.



回答3:

In case you don't know the keys yet:

You can just write a dict subclass, which will return the key if it was not found in the dictionary:

class FailsafeDict(dict):
    def __getitem__(self, item):
        try:
            return super().__getitem__(item)
        except KeyError:
            return "{" + str(item) + "}"
        # end try
    # end def
# end class

This works by overriding the __getitem__(item) function, which handles the dict[item] call.

You can than use it like this:

f = FailsafeDict({"yo": "lo", "foo": "bar"})
text = "{yo}! {lol}! {foo}!".format(**f)
print(text)  

Which prints lo! {lol}! bar!.

In your case that would be

msg = """...""".format(**FailsafeDict(practise_dict))