首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python ValueError:太多的值不能用字符串格式解压缩

Python ValueError:太多的值不能用字符串格式解压缩
EN

Stack Overflow用户
提问于 2015-06-11 21:02:21
回答 1查看 318关注 0票数 0

我使用Python字符串为电子邮件创建html,如下所示:

代码语言:javascript
复制
      # 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中)。

代码语言:javascript
复制
      # 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呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-11 21:04:24

width元素后面有一个未转义的%符号,添加另一个%来转义它:

代码语言:javascript
复制
  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时,您不再格式化字符串,%字符也不再特殊。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30791377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档