如何解码python的HTML代码中的变量名称

时间:2013-01-01 20:00:17

标签: 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"] = "test@company.com"
    msg["To"] = "test@company.com"
    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://username@company: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()

2 个答案:

答案 0 :(得分:4)

你想要的是

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

请注意,%符号位于元组之外。

答案 1 :(得分:0)

将百分号放在括号之外:

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