[英]Python ValueError: too many values to unpack with string formatting
我正在使用Python字符串为电子邮件创建html,如下所示:
# Code setting up the message html
message = "long html message string"
scoped = ""
if settings.DEBUG:
scoped = "scoped"
header = """
<style %s type='text/css'>
@media only screen and (max-width: 480px){
.emailImage{
height:auto !important;
max-width:200px !important;
width: 100% !important;
}
}
</style>
""" % scoped
footer = "html message footer"
message = header + message + footer
# Code sending the message.
问题是,上面的代码给我错误ValueError: too many values to unpack
。 但是,如果我从消息中删除scoped
变量,则html会运行,即可以正常工作(尽管没有按我的意愿将范围变量添加到HTML中)。
# Code setting up the message html
message = "long html message string"
header = """
<style type='text/css'>
@media only screen and (max-width: 480px){
.emailImage{
height:auto !important;
max-width:200px !important;
width: 100% !important;
}
}
</style>
"""
footer = "html message footer"
message = header + message + footer
# Code sending the message.
为什么第一个版本会引发该错误,如何解决ValueError?
在width
元素后有一个未转义的%符号,请添加另一个%以对其进行转义:
header = """
<style %s type='text/css'>
@media only screen and (max-width: 480px){
.emailImage{
height:auto !important;
max-width:200px !important;
width: 100%% !important;
}
}
</style>
""" % scoped
请注意,当摆脱% scoped
,您不再格式化字符串,并且%字符不再特殊。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.