我正在尝试将电子邮件模板放在一起。消息内容将取决于字典中的值。但是,字典可能每次都不包含所有键。
目前这很好,因为所有值都在字典中('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)
在msg
变量中,我正在尝试创建我的&#39;模板&#39;。这意味着我需要拥有可能在字典中的所有可能项目。
例如,下一篇文章会失败,因为它正在查找此词典中不存在的'Date'
:
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)
如果它没有作为查找字典中的键存在,是否有办法要求它忽略字符串替换?
答案 0 :(得分:3)
您可以使用Template
和safe_substitute
:
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)
<强>输出强>
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>
答案 1 :(得分:2)
我认为没有办法告诉format
某些替换是可选的。但是,这里有两个解决方法。
如果只有一个缺失的字段,请尝试类似:
msg = """...""".format(Date=practise_dict.pop("Date", ""), # handle optional field
**practise_dict) # pass the rest as before
但请注意,这会修改practise_dict
,如果要处理多个值,则会很尴尬。
如果可能缺少多个字段,或者您不想修改字典,则可能更可取以下内容:
defaults = {"Date": "", "Additional Details": ""} # define optional fields
defaults.update(practise_dict) # override user data where available
msg = """...""".format(**defaults)
对于您没有明确提供替代方法的密钥,这两种方法仍然会失败,这使您可以强制使用某些字段。
答案 2 :(得分:0)
您可以编写一个dict子类,如果在字典中找不到该键,它将返回该键:
.container {
width: 100%;
}
.col1 {
float: left;
width: 30%;
text-align: center;
}
.col2 {
float: left;
width: 70%;
}
table {
margin: 0 auto;
}
这可以通过覆盖class FailsafeDict(dict):
def __getitem__(self, item):
try:
return super().__getitem__(item)
except KeyError:
return "{" + str(item) + "}"
# end try
# end def
# end class
函数来处理__getitem__(item)
调用。
你可以这样使用它:
dict[item]
打印f = FailsafeDict({"yo": "lo", "foo": "bar"})
text = "{yo}! {lol}! {foo}!".format(**f)
print(text)
。
在你的情况下
lo! {lol}! bar!