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

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

在这篇文章中,我们将一步一步学习如何将定义的 Excel 范围转换为 Python 中的列表。

Python Excel to List Converter Library

开发人员可以通过 .NET 使用 Aspose.Cells for Python,而不是手动解析 Excel 文件,这是一款强大的 Excel 到列表转换库。它不仅使提取范围、行和列到 Python 列表变得更加容易,还支持公式、格式、图表和数据透视表等高级功能,确保即使在复杂电子表格中也能保持准确性。

在编码之前,确保你的设置已准备好:

  1. 安装 Python 3.7 及更高版本。
  2. 下载 Aspose.Cells 从 releases 或使用 pip 安装:
pip install aspose-cells-python
  1. 准备一个示例 Excel 文件 (sampledata.xlsx),内容如下:
将 Excel 转换为 Python 中的列表:示例数据文件

示例 Excel 数据文件。

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

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

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

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

以下Python脚本加载Excel文件,定义一个范围,并将其转换为Python列表。

from aspose.cells import Workbook

# 步骤 1:加载 Excel 工作簿
book = cells.Workbook("sample_data.xlsx")

# 步骤 2:访问第一个工作表
sheet1 = book.worksheets.get(0)

# 步骤 3:定义范围(A1:C4 在此示例中)
sheet_cells = sheet1.cells
range_obj = sheet_cells.create_range("A1", "C4")

# 步骤 4:将范围转换为嵌套的 Python 列表
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)

# 步骤 5:打印 Python 列表
print("Python List Output:")
print(range_list)

Output

Python List Output:
[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]

此完整脚本演示了如何从 Excel 中提取数据并将其转换为 Python 列表。之后,可以根据您的要求轻松转换为 Pandas 或 JSON。

将 Python 列表转换为 Pandas 数据框

使用 Pandas,您可以直接将列表转换为 DataFrame:

import pandas as pd

# Convert to a Pandas DataFrame
df = pd.DataFrame(range_list[1:], columns=range_list[0])
print(df)

熊猫数据框输出:

       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 列表中。
# 导入 Aspose.Cells 库
from aspose.cells import Workbook

# 步骤 1:从文件加载 Excel 工作簿
book = Workbook("sample_data.xlsx")

# 第 2 步:访问工作簿中的第一个工作表
sheet = book.worksheets.get(0)

# 步骤 3:定义行索引(0 = 第一行,其中包含标题)
row_index = 0
cells = sheet.cells

# 为选定行创建一个范围对象
row_range = cells.create_range(row_index, 0, 1, sheet.cells.max_column + 1)

# 步骤 4:将行转换为 Python 列表
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("Row to List:", row_list)

Output:

Row to List: ['City', 'Region', 'Store']

将 Excel 列转换为 Python 中的列表

您还可以将单列提取为列表。例如,让我们将地区列转换为列表:

  1. 加载工作簿和工作表。
  2. 选择按索引的列。
  3. 遍历列中的每一行。
  4. 将列值收集到列表中。
# 导入 Aspose.Cells 库
from aspose.cells import Workbook

# 第 1 步:从文件加载 Excel 工作簿
book = Workbook("sample_data.xlsx")

# 访问工作簿中的第一个工作表
sheet = book.worksheets.get(0)

# 步骤 2:定义列索引(0 = 第一列,即 A 列)
col_index = 0
cells = sheet.cells

# 为所选列创建一个范围对象
# 参数: (起始行, 起始列, 总行数, 总列数)
# 在这里,从第0行开始,选择列索引,包括所有行,宽度 = 1列
col_range = cells.create_range(0, col_index, sheet.cells.max_row + 1, 1)

# 第 3 步 & 4 步:将列转换为 Python 列表
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("Column to List:", col_list)

Output:

Column to List: ['City', 'Chicago', 'New York', 'Detroit']

获得免费许可证

评估 Aspose.Cells for Python via .NET,畅享无限制。请从 license page 请求免费的临时许可证。在您的代码中应用该许可证以移除评估限制。测试每个功能,包括 DF 到 Excel、图表、公式和大文件。

Excel 转列表:免费资源

利用以下资源来加深你的知识,增强你的理解,并获得实用的见解,帮助你更有效地应用所学。

结论

我们演示了如何通过 Aspose.Cells for .NET for Python 将 Excel 数据转换为 Python 列表,方法是提取范围、行和列。一旦数据变为列表形式,就可以用于 Pandas、JSON 或其他处理任务。虽然像 openpyxl 或 pandas.readexcel 这样的库可以提取范围,但 Aspose.Cells 提供了对公式、格式、图表和合并单元格的更高控制,使其成为复杂 Excel 操作的更好选择。

如果您需要帮助或有任何问题,请随时通过我们的 Aspose.Cells 免费支持论坛 联系我们。我们的团队将很乐意为您提供帮助。

另请参阅