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

如何在Excel中提取以Roll No开头的表格并存入数组temp

Hey there! Let's walk through how to extract your target Excel table into the temp = [] array, covering both scenarios you asked about, plus whether using Roll No as an identifier works:

1. Extracting Tables with Empty Rows/Irrelevant Metadata

The core idea is to first pinpoint the header row that contains Roll No and Name, then read all valid data rows from that point onward into your temp array. Here's a practical example using Python's openpyxl library:

from openpyxl import load_workbook

temp = []
wb = load_workbook("your_target_file.xlsx")
ws = wb.active

# Locate the header row
header_row = None
start_row = 0

for idx, row in enumerate(ws.iter_rows(values_only=True), start=1):
    # Check if the row contains our target header keywords
    if "Roll No" in row and "Name" in row:
        header_row = row
        start_row = idx + 1  # Next row is where the data starts
        break

# Read all data rows from the start row to the end
if start_row > 0:
    for row in ws.iter_rows(min_row=start_row, values_only=True):
        # Skip any remaining empty rows (if present)
        if any(cell is not None and cell.strip() != "" for cell in row):
            temp.append(list(row))

wb.close()

This code skips all pre-header metadata and empty rows, only pulling in the actual table data once it finds the Roll No/Name header.

2. Extracting Tables Without Empty Rows

Even if there are no empty rows, using the Roll No identifier is still the best approach. It avoids issues if there's any introductory text (like titles or notes) at the top of the file. The code above works perfectly here too—it'll just find the header row earlier in the iteration.

If you're 100% certain the table starts at the very first row, you could skip the header-locating step and start reading from row 1, but this is fragile (any future changes to the file structure will break it). Stick with the Roll No method for reliability.

3. Can We Use Roll No to Identify the Target Table?

Absolutely—and this is the most robust strategy available. Roll No acts as a unique, consistent marker for your target table, regardless of whether there are empty rows, metadata, or even other tables in the same file. It ensures you always pull the correct table, even if the file's layout changes slightly over time.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 16:40:51