原創(chuàng)|行業(yè)資訊|編輯:吉煒煒|2025-09-17 10:10:52.690|閱讀 46 次
概述:在 Python 中處理 Excel 數(shù)據(jù)通常需要將特定的行和列提取為列表格式。在本教程中,我們將逐步學(xué)習(xí)如何借助Aspose.Cells在 Python 中將定義的 Excel 范圍轉(zhuǎn)換為列表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在 Python 中處理 Excel 數(shù)據(jù)通常需要將特定的行和列提取為列表格式。將 Excel 范圍轉(zhuǎn)換為 Python 列表對(duì)于以下任務(wù)非常有用:
在本教程中,我們將逐步學(xué)習(xí)如何借助Aspose.Cells在 Python 中將定義的 Excel 范圍轉(zhuǎn)換為列表。
加入Aspose技術(shù)交流QQ群(1041253375),與更多小伙伴一起探討提升開(kāi)發(fā)技能。
開(kāi)發(fā)人員無(wú)需手動(dòng)解析 Excel 文件,而是可以使用 Aspose.Cells for Python via .NET(一個(gè)功能強(qiáng)大的 Excel 到列表轉(zhuǎn)換庫(kù))。它不僅可以更輕松地將范圍、行和列提取到 Python 列表中,還支持公式、格式、圖表和數(shù)據(jù)透視表等高級(jí)功能,即使在復(fù)雜的電子表格中也能確保準(zhǔn)確性。
在編碼之前,請(qǐng)確保您的設(shè)置已準(zhǔn)備就緒:
pip install aspose-cells-python
讓我們了解使用 Aspose.Cells for Python 將一系列 Excel 數(shù)據(jù)轉(zhuǎn)換為 Python 列表的過(guò)程。
按照以下步驟將 Excel 范圍轉(zhuǎn)換為 Python 中的列表:
以下 Python 腳本加載 Excel 文件,定義范圍并將其轉(zhuǎn)換為 Python 列表。
from aspose.cells import Workbook # Step 1: Load the Excel workbook book = cells.Workbook("sample_data.xlsx") # Step 2: Access the first worksheet sheet1 = book.worksheets.get(0) # Step 3: Define the range (A1:C4 in this example) sheet_cells = sheet1.cells range_obj = sheet_cells.create_range("A1", "C4") # Step 4: Convert the range into a nested Python list range_list = [] for row_index in range(range_obj.first_row, range_obj.first_row + range_obj.row_count): row = [] for column_index in range(range_obj.first_column, range_obj.first_column + range_obj.column_count): curr_cell = sheet_cells.check_cell(row_index, column_index) row.append(curr_cell.value if curr_cell else "") range_list.append(row) # Step 5: Print the Python list print("Python List Output:") print(range_list)輸出
Python List Output: [['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]
此完整腳本演示了如何從 Excel 中提取數(shù)據(jù)并將其轉(zhuǎn)換為 Python 列表。之后,您可以根據(jù)需要輕松地將其轉(zhuǎn)換為 Pandas 或 JSON。
使用 Pandas,您可以直接將列表轉(zhuǎn)換為 DataFrame:
import pandas as pd # Convert to a Pandas DataFrame df = pd.DataFrame(range_list[1:], columns=range_list[0]) print(df)Pandas DataFrame 輸出:
City Region Store 0 Chicago Central 3055 1 New York East 3036 2 Detroit Central 3074
您還可以將數(shù)據(jù)導(dǎo)出為 JSON:
import json # Convert to JSON json_output = json.dumps(range_list) print(json_output)JSON 輸出:
[["City", "Region", "Store"], ["Chicago", "Central", 3055], ["New York", "East", 3036], ["Detroit", "Central", 3074]]
有時(shí)您可能只想從 Excel 中提取一行并將其存儲(chǔ)為列表。以下是使用 Aspose.Cells 的操作方法:
# Import Aspose.Cells library from aspose.cells import Workbook # Step 1: Load the Excel workbook from file book = Workbook("sample_data.xlsx") # Step 2: Access the first worksheet in the workbook sheet = book.worksheets.get(0) # Step 3: Define the row index (0 = first row, which contains headers) row_index = 0 cells = sheet.cells # Create a range object for the selected row row_range = cells.create_range(row_index, 0, 1, sheet.cells.max_column + 1) # Step 4: Convert the row into a Python list row_list = [] for column_index in range(row_range.first_column, row_range.first_column + row_range.column_count): curr_cell = cells.check_cell(row_index, column_index) # Get each cell in the row row_list.append(curr_cell.value if curr_cell else "") # Append value or empty string if cell is blank # Print the extracted row as a list print("Row to List:", row_list)輸出:
Row to List: ['City', 'Region', 'Store']
您還可以將單列提取到列表中。例如,我們將“Region”列轉(zhuǎn)換為列表:
# Import Aspose.Cells library from aspose.cells import Workbook # Step 1: Load the Excel workbook from file book = Workbook("sample_data.xlsx") # Access the first worksheet in the workbook sheet = book.worksheets.get(0) # Step 2: Define the column index (0 = first column, i.e., Column A) col_index = 0 cells = sheet.cells # Create a range object for the selected column # Parameters: (start_row, start_column, total_rows, total_columns) # Here, start at row 0, select col_index, include all rows, and width = 1 column col_range = cells.create_range(0, col_index, sheet.cells.max_row + 1, 1) # Step 3 & 4: Convert the column into a Python list col_list = [] for row_index in range(col_range.first_row, col_range.first_row + col_range.row_count): curr_cell = cells.check_cell(row_index, col_index) # Get each cell in the column if curr_cell: # Only add if the cell exists (ignore empty rows) col_list.append(curr_cell.value) # Print the extracted column as a list print("Column to List:", col_list)輸出:
Column to List: ['City', 'Chicago', 'New York', 'Detroit']
我們演示了如何通過(guò) .NET 使用 Aspose.Cells for Python 提取范圍、行和列,將 Excel 數(shù)據(jù)轉(zhuǎn)換為 Python 列表。轉(zhuǎn)換為列表后,數(shù)據(jù)可用于 Pandas、JSON 或其他處理任務(wù)。雖然 openpyxl 或 pandas.read_excel 等庫(kù)可以提取范圍,但 Aspose.Cells 能夠更好地控制公式、格式、圖表和合并單元格,使其成為復(fù)雜 Excel 操作的更佳選擇。
————————————————————————————————————————
關(guān)于慧都科技:
慧都科技是專注軟件工程、智能制造、石油工程三大行業(yè)的數(shù)字化解決方案服務(wù)商。在軟件工程領(lǐng)域,我們提供開(kāi)發(fā)控件、研發(fā)管理、代碼開(kāi)發(fā)、部署運(yùn)維等軟件開(kāi)發(fā)全鏈路所需的產(chǎn)品,提供正版授權(quán)采購(gòu)、技術(shù)選型、個(gè)性化維保等服務(wù),幫助客戶實(shí)現(xiàn)技術(shù)合規(guī)、降本增效與風(fēng)險(xiǎn)可控。慧都科技Aspose在中國(guó)的官方授權(quán)代理商,提供Aspose系列產(chǎn)品免費(fèi)試用,咨詢,正版銷售等于一體的專業(yè)化服務(wù)。Aspose是文檔處理領(lǐng)域的優(yōu)秀產(chǎn)品,幫助企業(yè)高效構(gòu)建文檔處理的應(yīng)用程序。
下載|體驗(yàn)更多Aspose產(chǎn)品,請(qǐng)咨詢,或撥打產(chǎn)品熱線:023-68661681
加入Aspose技術(shù)交流QQ群(1041253375),與更多小伙伴一起探討提升開(kāi)發(fā)技能。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)