2012-03-26 65 views
7

我試圖讓一些HTML來處理我的Python代碼。 我有一個我的CSS代碼。Python和HTML'%Operator'

#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的其他地方。

任何幫助將不勝感激。

+1

你如何「訪問」該頁面? – 2012-03-26 13:01:29

+0

好的,你有沒有測試過如果你刪除'%'(用'px'或其他)代替''''。錯誤消失了嗎? – 2012-03-26 13:10:01

回答

19

如果要使用%格式化運算符,則需要跳過%字符。

所以你的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}。當然,在這種情況下,你也需要逃脫你的{}

+0

這是什麼意思? – user432584920684 2012-03-26 13:04:02

+5

您的答案在技術上是正確的,正如我們都知道的是最好的一種正確答案。 – cha0site 2012-03-26 13:04:30

+0

非常感謝您的幫助。 – user432584920684 2012-03-26 13:11:49