2013-01-01 134 views
1

我在下面的代碼中得到了下面的編譯錯誤,發生錯誤的地方是正在解碼變量名的代碼?如何解碼變量名HTML代碼?如何解碼python的HTML代碼中的變量名

File "test.py", line 33 
    """)(% hostname,change,pwd,changeref,changeref) 
     ^
SyntaxError: invalid syntax 

以下是代碼: -

from email.mime.text import MIMEText 
from subprocess import Popen, PIPE 
import socket 
import os 

def email (body,subject): 
    msg = MIMEText("%s" % body) 
    msg["Content-Type"] = "text/html" 
    msg["From"] = "[email protected]" 
    msg["To"] = "[email protected]" 
    msg["Subject"] = '%s' % subject 
    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE) 
    p.communicate(msg.as_string()) 
def main(): 
    change="2009" 
    pwd=os.getcwd() 
    changeref="refs/changes/59/206059/2" 
    subject=change + "Test subject" 
    hostname=socket.gethostname() 
    body = ("""\ 
     <html> 
     <head></head> 
     <body> 
      <p>%s<br> 
      change - https://company/#/c/%s \n.<br> 
      change Directory - %s for details \n.<br> 
      Instructions: cd to the change Directory on hostname and execute the following commands \n.<br> 
      git checkout %s<br> 
      git fetch ssh://[email protected]:29418/platform/vendor/company-proprietary/code %s && git cherry-pick FETCH_HEAD". 
     </p> 
     </body> 
     </html> 
""")(% hostname,change,pwd,changeref,changeref) 
    email(body,subject) 

if __name__ == '__main__': 
    main() 
+0

如果可以的話,請用新的樣式字符串格式化失去醜陋。 – root

回答

4

你想要的是

""" 
your HTML stuff here 
""" % (hostname, change, pwd, changeref, changeref) 

注意,%符號而來的元組之外。

0

把百分號了括號:

  </body> 
     </html> 
""") % (hostname,change,pwd,changeref,changeref)