2011-01-19 11 views
1

)pythonでlocals()を使用して文字列を変数に置き換えようとしましたが、文字をエラーなしで使用できます。ここでは具体的な例である:"%"文字列原因ロケールでの文字列置換のエラー(

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