当前位置: 首页 > news >正文

Excel处理控件Aspose.Cells教程:如何将Excel区域转换为Python列表

在 Python 中处理 Excel 数据通常需要将特定的行和列提取为列表格式。将 Excel 范围转换为 Python 列表对于以下任务非常有用:

  • 使用PandasNumPy进行数据分析
  • 报告和 ETL 流程的自动化
  • 与机器学习模型或 API 集成

在本教程中,我们将逐步学习如何借助Aspose.Cells在 Python 中将定义的 Excel 范围转换为列表。

Aspose.Cells官方试用版免费下载

Python Excel 到列表转换库

开发人员无需手动解析 Excel 文件,而是可以使用 Aspose.Cells for Python via .NET(一个功能强大的 Excel 到列表转换库)。它不仅可以更轻松地将范围、行和列提取到 Python 列表中,还支持公式、格式、图表和数据透视表等高级功能,即使在复杂的电子表格中也能确保准确性。

在编码之前,请确保您的设置已准备就绪:

  1. 安装Python 3.7+
  2. 从发行版下载 Aspose.Cells或使用 pip 安装:
    pip install aspose-cells-python
  3. sample_data.xlsx准备一个包含以下内容的示例 Excel 文件( ):

将 Excel 范围转换为 Python 列表:分步指南

让我们了解使用 Aspose.Cells for Python 将一系列 Excel 数据转换为 Python 列表的过程。

按照以下步骤将 Excel 范围转换为 Python 中的列表:

  1. 首先,使用该类加载现有的 Excel 文件Workbook。
  2. 其次,获取第一个工作表。
  3. 接下来,创建一个范围,例如 A1 到 C4。
  4. 之后,将 Range 转换为 Python List。
  5. 最后,打印列表。

以下 Python 脚本加载 Excel 文件,定义范围并将其转换为 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 中提取数据并将其转换为 Python 列表。之后,您可以根据需要轻松地将其转换为 Pandas 或 JSON。

将 Python 列表转换为 Pandas DataFrame

使用 Pandas,您可以直接将列表转换为 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

将 Python 列表保存为 JSON

您还可以将数据导出为 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]]

在 Python 中将 Excel 行转换为列表

有时您可能只想从 Excel 中提取一行并将其存储为列表。以下是使用 Aspose.Cells 的操作方法:

  1. 加载 Excel 工作簿。
  2. 访问目标工作表。
  3. 通过索引选择行。
  4. 将行值收集到 Python 列表中。
# 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 rowrow_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']

使用 Python 将 Excel 列转换为列表

您还可以将单列提取到列表中。例如,我们将“Region”列转换为列表:

  1. 加载工作簿和工作表。
  2. 通过索引选择列。
  3. 遍历列中的每一行。
  4. 将列值收集到列表中。
# 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 columnif 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']

结论

我们演示了如何通过 .NET 使用 Aspose.Cells for Python 提取范围、行和列,将 Excel 数据转换为 Python 列表。转换为列表后,数据可用于 Pandas、JSON 或其他处理任务。虽然 openpyxl 或 pandas.read_excel 等库可以提取范围,但 Aspose.Cells 能够更好地控制公式、格式、图表和合并单元格,使其成为复杂 Excel 操作的更佳选择。


文章转载自:

http://vo8oTPp8.rbffj.cn
http://v8VgiYqr.rbffj.cn
http://FUkF0mfR.rbffj.cn
http://vcNPGl1W.rbffj.cn
http://uNx4V2yJ.rbffj.cn
http://Qt2K9Dxe.rbffj.cn
http://yWve49X3.rbffj.cn
http://RP2EIJ2r.rbffj.cn
http://lWlyYeaQ.rbffj.cn
http://BRSweZ5o.rbffj.cn
http://6rby1Xaw.rbffj.cn
http://9aFe6VkL.rbffj.cn
http://1H8jTfJo.rbffj.cn
http://oZnIKndk.rbffj.cn
http://3nRf80Vd.rbffj.cn
http://6xqBKj4O.rbffj.cn
http://K79Defix.rbffj.cn
http://ur8PZUzT.rbffj.cn
http://z2QasbLa.rbffj.cn
http://LDUaQWVz.rbffj.cn
http://xx3HwwqS.rbffj.cn
http://yQYU65wd.rbffj.cn
http://eKN5RHyY.rbffj.cn
http://FxmXjE12.rbffj.cn
http://tsCMkpPq.rbffj.cn
http://887THarZ.rbffj.cn
http://1I9ovPta.rbffj.cn
http://U0Ygr2oT.rbffj.cn
http://FPC3Cb7s.rbffj.cn
http://f4grDXB9.rbffj.cn
http://www.dtcms.com/a/388201.html

相关文章:

  • Java 实现 Excel 与 TXT 文本高效互转
  • 【vue+exceljs+file-saver】纯前端:下载excel和上传解析excel
  • 国产化Excel开发组件Spire.XLS教程:使用 Python 设置 Excel 格式,从基础到专业应用
  • Parasoft以高标准测试助力AEW提升汽车软件质量
  • el-date-picker时间选择器限制时间跨度为3天
  • 35.Socket网络编程(UDP)(下)
  • 【前沿技术Trip Three】正则表达式
  • 多平台数据交换解耦方案选型
  • ​​[硬件电路-239]:从电阻器的高频等效模型,看高频信号的敏感性,电路的性能受到频率的影响较大
  • Java 中的 23 种设计模式详解
  • 《2025年AI产业发展十大趋势报告》六十二
  • 【字节跳动】LLM大模型算法面试题:大模型 LLM的架构介绍?
  • 【C++】类成员访问控制
  • 彩笔运维勇闯机器学习--梯度下降法
  • 正点原子zynq_FPGA学习笔记-vivado安装
  • 基于yolov8/yolo11的视觉识别算法使用和详解
  • 2025年数据科学与大数据技术和统计学有什么区别?
  • STM32H743-ARM例程2-GPIO点亮LED
  • 每天五分钟深度学习:深层神经网络的前向传播算法和反向传播算法
  • 【LeetCode】41. 缺失的第一个正数
  • Linux系统指令之 —— ip route route
  • 嵌入式硬件笔记:三种滤波电路的对比
  • webrtc弱网-InterArrivalDelta类源码分析与算法原理
  • 第6章:计算机内存实战
  • 模型压缩与量化实战:将BERT模型缩小4倍并加速推理
  • RS485 与 CAN 通讯:选哪个更合适?
  • 腾讯微保社招笔试
  • centos系统安装mysql8
  • Go语言垃圾回收器深入解析
  • 大模型的领域知识注入的四种路径