Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将数据添加到html变量_Python_Python 2.7 - Fatal编程技术网

在python中将数据添加到html变量

在python中将数据添加到html变量,python,python-2.7,Python,Python 2.7,html文件 TEMPLATE2 = """ <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <style> table, th, td { border: 1px solid black; } </style> <

html文件

TEMPLATE2 = """
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <br />
    <table width="auto">
        <tr>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr1  </span><br /> </td>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr2</span><br /> </td>
        </tr>
        <tr> <td>1stRow:</td><td>{1strowVal}</td></tr>
        ...
        ...
        ...
        <tr><td>25throw</td><td>{25throwVal}</td></tr>
    </table>
</body>
</html>
"""
try2

尝试3


任何建议

Python变量名都不能以整数开头,因此需要将1strowVal更改为更像rowVal1的值。接下来,使用格式字符串将变量注入字符串:

rowVal1 = "SOME_STRING_VALUE"
rowVal25 = "SOME_STRING_VALUE"

TEMPLATE2 = """
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <br />
    <table width="auto">
        <tr>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr1  </span><br /> </td>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr2</span><br /> </td>
        </tr>
        <tr> <td>1stRow:</td><td>%s</td></tr>
        ...
        ...
        ...
        <tr><td>25throw</td><td>%s</td></tr>
    </table>
</body>
</html>
""" % (rowVal1, rowVal25)
rowVal1=“一些字符串值”
rowVal25=“一些字符串值”
模板2=“”
表,th,td{
边框:1px纯黑;
}

Hdr1
Hdr2
1stRow:%s ... ... ... 25次投掷%s “%”(第1行,第25行)
您的意思是要用第n个值替换字符串
“{NthrowVal}”
(例如,
“{25throwVal}”
)?价值从何而来?它的数据类型是什么?我想用{NthrowVal}替换它,数据类型是string#DHZconverted我的另一个代码逻辑,以便像您提到的那样一次加载所有值
s = Template(TEMPLATE2).safe_substitute(NthrowVal="Alex")
  msg = MIMEText(
       Environment().from_string(TEMPLATE2).render(
      NthrowVal="someval"
       ), "html"
    )
rowVal1 = "SOME_STRING_VALUE"
rowVal25 = "SOME_STRING_VALUE"

TEMPLATE2 = """
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <br />
    <table width="auto">
        <tr>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr1  </span><br /> </td>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr2</span><br /> </td>
        </tr>
        <tr> <td>1stRow:</td><td>%s</td></tr>
        ...
        ...
        ...
        <tr><td>25throw</td><td>%s</td></tr>
    </table>
</body>
</html>
""" % (rowVal1, rowVal25)