You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

零基础如何用Python编写无依赖代码输出HTML表格?

How to Generate an HTML Table with Basic Python (No Modules Needed)

Hey there! Totally get where you're coming from—starting out with programming and having to build HTML without any fancy tools can feel overwhelming at first, but it’s actually way simpler than you think. You’re spot-on with the idea of writing <tr>, <td> and other tags directly in your Python code using print()! Let’s break this down step by step.

The Core Concept: HTML is Just Plain Text

At its core, HTML is just structured plain text. So instead of using modules to generate it, we can use Python’s basic tools (like print() and loops) to output those tags exactly as we need them. No extra libraries required—just what you’ve already learned!

Step 1: Refresh on HTML Table Structure

First, let’s make sure we know the basic building blocks of an HTML table:

  • The whole table lives inside <table> and </table> tags.
  • Each row is wrapped in <tr> (table row) tags.
  • Regular cells use <td> (table data) tags; header cells (like column titles) use <th> (table header) tags.

Step 2: Static Table Example (Manual Output)

Let’s start with a simple, hardcoded table to get the hang of it. Save this as table.py:

# Start the table (border='1' adds visible borders for testing)
print("<table border='1'>")

# Header row
print("  <tr>")
print("    <th>姓名</th>")
print("    <th>专业</th>")
print("    <th>成绩</th>")
print("  </tr>")

# First data row
print("  <tr>")
print("    <td>张三</td>")
print("    <td>计算机科学</td>")
print("    <td>90</td>")
print("  </tr>")

# Second data row
print("  <tr>")
print("    <td>李四</td>")
print("    <td>软件工程</td>")
print("    <td>85</td>")
print("  </tr>")

# End the table
print("</table>")

Run it in bash with python table.py, and you’ll see the full HTML code printed in your terminal. To view the actual table, copy the output into a file named table.html and open it in any browser—you’ll see your table right there!

Step 3: Dynamic Table Example (Using Loops)

Writing every row manually isn’t efficient. Let’s use loops to generate rows from a dataset—this is where your existing knowledge of for loops really shines:

# Sample dataset (a 2D list: each sublist is a row of data)
student_data = [
    ["王五", "电子信息", 88],
    ["赵六", "人工智能", 92],
    ["孙七", "网络工程", 83]
]

# Start the table
print("<table border='1'>")

# Print the header row
print("  <tr>")
headers = ["姓名", "专业", "成绩"]
for header in headers:
    print(f"    <th>{header}</th>")
print("  </tr>")

# Print data rows with nested loops
for student in student_data:
    print("  <tr>")
    # Loop through each item in the row to create cells
    for item in student:
        # Use an f-string to insert the item into the <td> tag
        # If you haven't learned f-strings yet, use string concatenation: "<td>" + str(item) + "</td>"
        print(f"    <td>{item}</td>")
    print("  </tr>")

# End the table
print("</table>")

Quick Notes to Keep in Mind:

  • Indentation: The spaces before <tr> and <td> aren’t required for the table to work, but they make the outputted HTML code easier to read (and debug if needed).
  • String Conversion: If your data includes numbers (like grades), make sure to convert them to strings when using manual concatenation (the f-string does this automatically for you).
  • Flexibility: Just update the student_data list with new rows, and the loops will handle generating all the corresponding HTML tags—no need to rewrite every line!

Final Tips

Start with the static example first to get comfortable with how the HTML tags fit together, then move to the dynamic version. Since you already know print() and loops, this is just applying those basics to output specific text (the HTML tags). You’ve got this!

内容的提问来源于stack exchange,提问作者flo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:47:29