没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|行业资讯|编辑:吉炜炜|2025-09-17 10:10:52.690|阅读 46 次
概述:在 Python 中处理 Excel 数据通常需要将特定的行和列提取为列表格式。在本教程中,我们将逐步学习如何借助Aspose.Cells在 Python 中将定义的 Excel 范围转换为列表。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
在 Python 中处理 Excel 数据通常需要将特定的行和列提取为列表格式。将 Excel 范围转换为 Python 列表对于以下任务非常有用:
在本教程中,我们将逐步学习如何借助Aspose.Cells在 Python 中将定义的 Excel 范围转换为列表。
加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。
开发人员无需手动解析 Excel 文件,而是可以使用 Aspose.Cells for Python via .NET(一个功能强大的 Excel 到列表转换库)。它不仅可以更轻松地将范围、行和列提取到 Python 列表中,还支持公式、格式、图表和数据透视表等高级功能,即使在复杂的电子表格中也能确保准确性。
在编码之前,请确保您的设置已准备就绪:
pip install aspose-cells-python
让我们了解使用 Aspose.Cells for Python 将一系列 Excel 数据转换为 Python 列表的过程。
按照以下步骤将 Excel 范围转换为 Python 中的列表:
以下 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。
使用 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
您还可以将数据导出为 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]]
有时您可能只想从 Excel 中提取一行并将其存储为列表。以下是使用 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”列转换为列表:
# 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']
我们演示了如何通过 .NET 使用 Aspose.Cells for Python 提取范围、行和列,将 Excel 数据转换为 Python 列表。转换为列表后,数据可用于 Pandas、JSON 或其他处理任务。虽然 openpyxl 或 pandas.read_excel 等库可以提取范围,但 Aspose.Cells 能够更好地控制公式、格式、图表和合并单元格,使其成为复杂 Excel 操作的更佳选择。
————————————————————————————————————————
关于慧都科技:
慧都科技是专注软件工程、智能制造、石油工程三大行业的数字化解决方案服务商。在软件工程领域,我们提供开发控件、研发管理、代码开发、部署运维等软件开发全链路所需的产品,提供正版授权采购、技术选型、个性化维保等服务,帮助客户实现技术合规、降本增效与风险可控。慧都科技Aspose在中国的官方授权代理商,提供Aspose系列产品免费试用,咨询,正版销售等于一体的专业化服务。Aspose是文档处理领域的优秀产品,帮助企业高效构建文档处理的应用程序。
下载|体验更多Aspose产品,请咨询,或拨打产品热线:023-68661681
加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都网在现代工业自动化的发展过程中,高效、可靠且直观的监控系统已成为工厂与基础设施管理的核心系统。GENESIS64作为业界领先的工业可视化监控平台,以其一体化HMI/SCADA解决方案,为企业提供了从车间到管理层的全方位数据洞察与控制能力。
Prosys OPC UA SDK for Java支持在Android上开发OPC UA应用程序。
本文将为大家介绍界面导航组件QtitanNavigation在工业制造中的落地应用探索,欢迎下载最新版体验!
通过HOOPS Exchange的支持,CDM Tech在相对短的时间内取得了显著进展。
专业的电子表格控件,无需MS Excel也可满足一切Excel表格功能。
Aspose.Cells专业的电子表格控件,无需MS Excel也可满足一切Excel表格功能。
Spire.XLS for PythonSpire.XLS for Python是一个专业的 Excel 开发组件
IronXL直观的C#和VB.NET Excel API ,不需要安装MS Office或Excel In
Aspose.TotalAspose.Total 能为.NET和JAVA应用程序增加图表、电子邮件、拼写检查、条码、流程、文件格式管理等功能。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢