如何通过Python循环列表数据生成单张HTML表格
Generate Single HTML Table from Python Device Lists
Got it, let's fix this up for you. You want to automate creating a single HTML table with all your device data instead of manually building multiple tables. Here's a straightforward Python solution that does exactly that:
Step-by-Step Code
# Your device data grouped into a single list of lists device_data = [ ['N3132-a', '172.20.138.18', '192.168.193.15/24', 'N3132-a.td.com ', 'N3K-C3132Q-40GX', 'FOC2116XCFV', 'NXOS: version 7.0(3)I4(6)'], ['N56k-a', '10.249.98.11', '192.168.173.16/24', 'N56k-a.td.com ', 'N5K-C56128P', 'FOC2187FGHJ', 'kickstart: version 7.1(4)N1(1)'], ['N3164-b', '172.16.48.14', '192.168.130.12/24', 'N3164-b.td.com ', 'N3K-C3164Q-40GE', 'FDO26HJKL2', 'NXOS: version 7.0(3)I4(6)'], ['5596-a', '10.250.98.20', '192.168.170.25/24', '5596-a.td.com ', 'N5K-C5596UP', 'FOX2398HGF', 'kickstart: version 7.1(4)N1(1)'], ['N3064-B', '172.17.47.20', '192.168.130.25/24', 'N3064-B.td.com ', 'N3K-C3064PQ-10GX', 'FOC193FGHJNM', 'NXOS: version 7.0(3)I4(6)'] ] # Define the HTML structure template html_start = """<HTML> <head> <title>Data</title> </head> <body> <h1><marquee>Welcome To The Page</marquee></h1> <h1>Data :</h1> <table border="4" align="center"> <th style="background-color:yellow !important;"><h2><br>dc</h2></th> <th style="background-color:yellow !important;"><h2><br>proxi</h2></th> <th style="background-color:yellow!important;"><h2><br>id</h2></th> <th style="background-color:yellow !important;"><h2><br>email</h2></th> <th style="background-color:yellow!important;"><h2><br>face-1</h2></th> <th style="background-color:yellow!important;"><h2><br>face-2</h2></th> <th style="background-color:yellow!important;"><h2><br>face-3</h2></th> """ # Build the table rows by looping through each device table_rows = "" for device in device_data: # Convert each device's attributes into <td> elements td_elements = [f"<td>{item}</td>" for item in device] # Combine into a single table row table_rows += f"<tr>{''.join(td_elements)}</tr>\n" # Close the HTML structure html_end = """</table> </body> </HTML>""" # Combine all parts into the final HTML string final_html = html_start + table_rows + html_end # Write the HTML to a file with open("device_table.html", "w") as file: file.write(final_html) print("HTML table generated successfully! Check device_table.html")
How This Works
- Group Your Data: We first put all your individual device lists into one master list (
device_data) so we can loop through them easily. - HTML Template: We define the static part of your HTML (head, title, marquee, table headers) upfront—this matches the structure you provided in your manual example.
- Generate Rows: For each device in your data, we convert its 7 attributes into
<td>tags, wrap them in a<tr>(table row), and append this to our row string. - Assemble & Save: We stitch together the static HTML, generated rows, and closing tags, then write everything to an HTML file (
device_table.html).
When you run this script, you'll get a single HTML file with all your devices in one table—no more manual copying and pasting!
内容的提问来源于stack exchange,提问作者naruto




