如果foo中不存在键,则忽略str.format(** foo) [英] Ignore str.format(**foo) if key doesn't exist in foo
问题描述
我正在尝试一起获取电子邮件模板。消息内容将取决于字典中的值。但是,字典每次可能不包含所有的键。
目前这个都很好,所有的值都在字典中('Title' / code>,
'姓'
,'附加详情'
):
practise_dict = {其他详细信息:blah blah blah blah,Title:Mr,Surname:Smith :/ test / tester}
msg =来自:John Smith< no-reply@somethingsomething.co.uk>
至:{Title} {Surname} < blah@blahblah.co.uk>
MIME版本:1.0
内容类型:text / html
主题:新网站查询
这是一封电子邮件以HTML格式发送
{附加详情}
< b>这是HTML消息。< / b>
< h1>这是标题。< / h1> $
$ b 在 msg
变量中,我试图创建我的模板。这意味着我需要将所有可能的项目都放在字典中。
例如,下一块将失败,因为它正在寻找 日期
在此字典中不存在:
practise_dict = {其他详细信息 :blah blah blah blah,Title:Mr,Surname:Smith,URL:/ test / tester}
msg = John Smith< no-reply@somethingsomething.co.uk>
至:{Title} {Surname}< blah@blahblah.co.uk>
MIME版本:1.0
内容类型:text / html
主题:新的网站查询
这是一个以HTML格式发送的电子邮件
{附加详情}
{Date}
< b>这是HTML消息。< / b>
< h1>这是标题< / h1>
`.format(** practise_dict)
print(msg)
有没有办法让它忽略字符串替换如果它不存在作为查询字典中的键?
解决方案您可以使用模板
和 safe_substitute
/ p>
从字符串导入模板
practise_dict = {Additional_Details:blah blah blah blah,标题:Mr,Surname:Smith,URL:/ test / tester}
msg =来自:John Smith< no-reply @ somethingsomething .co.uk>
至:$ Title $ Surname< blah@blahblah.co.uk>
MIME版本:1.0
内容类型:text / html
主题:新网站查询
这是一封以HTML格式发送的电子邮件
$ Additional_Details
$ Date
< b>这是HTML消息。< / b>
< h1>这是标题。< / h1>
`
s =模板(msg).safe_substitute(** practise_dict)
打印
OUTPUT
From:John史密斯< no-reply@somethingsomething.co.uk>
至:史密斯先生< blah@blahblah.co.uk>
MIME版本:1.0
内容类型:文字/ html
主题:新的网站查询
这是一个以HTML格式发送的电子邮件
blah blah blah blah
$ Date
< b> This是HTML消息。< / b>
< h1>这是标题。< / h1>
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?
解决方案 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>
这篇关于如果foo中不存在键,则忽略str.format(** foo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!