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

如何用Python 3.7.5将给定列表输出为无符号表格格式?

Convert Student List to Clean Table Format in Python 3.7.5

Got it, let's get your student data into a clean, brace-free table format—here's exactly how to do it with Python 3.7.5.

First, let's tackle the core problem: stripping out those curly braces and quotes, and structuring the data into a proper table. We'll cover two practical approaches: a simple tab-separated table (great for spreadsheets) and a polished aligned ASCII table (for readability).

Simple Tab-Separated Table

This is quick, lightweight, and perfect for copying into tools like Excel or Google Sheets:

students = [
    {"grade": 5, "firstname": "Brendon", "lastname": "Urie", "gender": "M"},
    {"grade": 7, "firstname": "Freddy", "lastname": "Mercury", "gender": "M"},
    {"grade": 12, "firstname": "Tessa", "lastname": "Thompson"}
]

# Collect all unique column headers from all student entries
headers = set()
for student in students:
    headers.update(student.keys())
headers = sorted(headers)  # Optional: sort headers alphabetically

# Generate and print the table
print("\t".join(headers))
print("\t".join(["-" * len(header) for header in headers]))

for student in students:
    row = [str(student.get(header, "N/A")) for header in headers]
    print("\t".join(row))

Example Output:

firstname	grade	lastname	gender
---------	-----	--------	------
Brendon	5	Urie	M
Freddy	7	Mercury	M
Tessa	12	Thompson	N/A

Polished Aligned ASCII Table

If you want a more visually readable table with neatly aligned columns, use this approach:

students = [
    {"grade": 5, "firstname": "Brendon", "lastname": "Urie", "gender": "M"},
    {"grade": 7, "firstname": "Freddy", "lastname": "Mercury", "gender": "M"},
    {"grade": 12, "firstname": "Tessa", "lastname": "Thompson"}
]

# Get all headers and calculate column widths for perfect alignment
headers = sorted(set(k for student in students for k in student.keys()))
col_widths = [
    max(len(str(student.get(header, ""))) for student in students + [{"dummy": header}])
    for header in headers
]

# Build the table structure
header_line = " | ".join(f"{header:<{col_widths[i]}}" for i, header in enumerate(headers))
separator_line = "-+-".join(["-" * width for width in col_widths])

# Print the finished table
print(header_line)
print(separator_line)
for student in students:
    row_line = " | ".join(
        f"{str(student.get(header, 'N/A')):<{col_widths[i]}}"
        for i, header in enumerate(headers)
    )
    print(row_line)

Example Output:

firstname | grade | lastname | gender
--------- | ----- | -------- | ------
Brendon   | 5     | Urie     | M
Freddy    | 7     | Mercury  | M
Tessa     | 12    | Thompson | N/A

Integrating with Your output Function

If your output("list", students, None) is a custom function, you can adapt the code to pass the table as a formatted string instead. For example:

# Build the table into a single string
table_content = [header_line, separator_line]
for student in students:
    row_line = " | ".join(
        f"{str(student.get(header, 'N/A')):<{col_widths[i]}}"
        for i, header in enumerate(headers)
    )
    table_content.append(row_line)
final_table = "\n".join(table_content)

# Replace your original output call with this
output("table", final_table, None)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:17:16