【问题标题】:Python and HTML '% Operator'Python 和 HTML '% 运算符'
【发布时间】:2012-04-09 23:34:49
【问题描述】:

我正在尝试让一些 HTML 与我的 python 代码一起工作。 这是我的一个 CSS 代码。

#footerBar {
height: 40px;
background: red;
position: fixed;
bottom: 0;
width: 100%;
z-index: -1;
}

但是,当我尝试访问该页面时,出现以下错误。

File "projv2.py", line 151, in welcome
</form>""" %(retrievedFullName, retrievedUserName,)
ValueError: unsupported format character ';' (0x3b) at index 1118

我认为它与 % 混淆了,因为我确实在 HTML 的其他地方使用了它。

任何帮助将不胜感激。

【问题讨论】:

  • 你是如何“访问”那个页面的?
  • 好的,你有没有测试过如果你删除%(替换为px或其他)会发生什么。错误会消失吗?

标签: python html css


【解决方案1】:

如果你想使用% 格式化操作符,你需要转义你的% 字符。

所以你的 css 应该是:

#footerBar {
height: 40px;
background: red;
position: fixed;
bottom: 0;
width: 100%%;
z-index: -1;
}

改为。

最好使用字符串的.format() 方法,因为它是更好的方法。请参阅PEP 3101 了解基本原理。

所以不是

...""" % (retrievedFullName, retrievedUserName,)

...""".format(retrievedFullName, retrievedUserName)

并将字符串中的%s 更改为{0}{1}。当然,在这种情况下,您也需要转义您的 {}

【讨论】:

  • 您的回答在技术上是正确的,众所周知,这是最好的正确答案。
  • 非常感谢您的帮助。
  • (+1,因为您已经彻底讨论了格式字符串中的转义 =])
  • @Kimvais 在我的代码.format 中与%s 一起工作,所以我是否需要将%s 更改为{}
  • @GrijeshChauhan 不需要这样做只是为了更改,但是如果/当您确实需要对其进行更改时,不妨切换到.format()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2018-09-19
  • 2019-07-04
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
相关资源
最近更新 更多