【问题标题】:Python html format all select table rowPython html格式全部选择表格行
【发布时间】:2020-09-07 21:27:14
【问题描述】:
cursor.execute("""SELECT [APPL_NAME] ,[JOB_NAME],[LABE] FROM XYZ)
         resultat = cursor.fetchall()
         for row in resultat:
                html_code = """
                 <!doctype html>
             <html>
             <head>
             <meta charset="utf-8">
             <title>Untitled Document</title>
             <body>
                 <table border='1'
                 <tr>
                     <th>Date</th>
                     <th>Count</th>
                     <th>Status</th>
                 </tr>
                 <tr>
                     
                     <td>{}</td>
                     <td>{}</td>
                     <td>{}</td>
                 </tr>
                 </table>
                 </body>
                 </html>""".format(row[0],row[1],row[3])

我正在尝试从数据库表中获取记录并通过电子邮件发送数据。在表中选择查询 10-20 记录。但是在电子邮件中只有一条记录打印在上面的代码表中。 我尝试了列表中的所有数据,但没有运气。请建议我如何格式化电子邮件中的所有选择表记录。谢谢

【问题讨论】:

    标签: python python-3.x list python-2.7 python-requests


    【解决方案1】:

    你的算法不好。在循环中,您替换所有 html 代码而不是添加新行。

    cursor.execute("""SELECT [APPL_NAME],[JOB_NAME],[LABE] FROM XYZ""")
    resultat = cursor.fetchall()
    

    创建行:

    rows_code = ""
    for row in resultat:
        rows_code = rows_code + """
            <tr>
                 <td>{}</td>
                 <td>{}</td>
                 <td>{}</td>
            </tr>
        """.format(row[0],row[1],row[2])
    

    创建完整的 html 代码:

    html_code = """
        <!doctype html>
         <html>
         <head>
         <meta charset="utf-8">
         <title>Untitled Document</title>
         <body>
             <table border='1'
             <tr>
                 <th>Date</th>
                 <th>Count</th>
                 <th>Status</th>
             </tr>
             {}
             </table>
             </body>
             </html>
    """.format(rows_code)
    

    【讨论】:

    • ('row index out of range index=3 len=3',) 您好,感谢您的代码。但我面临索引超出范围的问题。你能建议更多吗
    猜你喜欢
    • 1970-01-01
    • 2013-10-16
    • 2013-05-09
    • 2019-01-07
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多