2011-01-19 53 views
1

我嘗試substitue串用當地人()在python變量,但我能找到一種方法,使用字符串內字符沒有錯誤。下面是一個具體的例子:「%」字符錯誤原因與當地人()

color = colors_generator() #the function return a color 

html = """<html><head> 
<style>#square{color:%(color)s;width:100%;height:100%;}</style>  
</head> <body> <div id="square"> </div> 
</body></html>""" % locals() 

print "Content-Type: text/html\n"  
print html 

結果:TypeError: not enough arguments for format string

的問題是在100%字符。我怎麼能逃脫它?

回答

4

逃生%和%

html = """<html><head> 
<style>#square{color:%(color)s;width:100%%;height:100%%;}</style>  
</head> <body> <div id="square"> </div> 
</body></html>""" % locals() 
+0

快速有效的,它的工作原理!謝謝 – Thammas 2011-01-19 20:47:22

相關問題