做律师百度推广的网站,网站备案号怎么申请,宣传网站有哪些,seo公司 彼亿营销文章目录 1 引言2 使用 openpyxl2.1 安装 openpyxl2.2 写入 Excel 文件2.3 读取 Excel 文件 3 使用 pandas3.1 安装 pandas 和 openpyxl3.2 写入 Excel 文件3.3 读取 Excel 文件 4 实例演示4.1 安装所需库4.2 封装为excel_example.py脚本文件 5 注意事项6 总结 1 引言
在现代办… 文章目录 1 引言2 使用 openpyxl2.1 安装 openpyxl2.2 写入 Excel 文件2.3 读取 Excel 文件 3 使用 pandas3.1 安装 pandas 和 openpyxl3.2 写入 Excel 文件3.3 读取 Excel 文件 4 实例演示4.1 安装所需库4.2 封装为excel_example.py脚本文件 5 注意事项6 总结 1 引言
在现代办公自动化中Excel 文件广泛应用于数据存储、分析和报告。Python 作为一个强大的编程语言提供了多个库来处理 Excel 文件其中最受欢迎的是 openpyxl 和 pandas。本文将深入介绍如何在 Python 中使用这些库来读写 Excel 文件。
2 使用 openpyxl
openpyxl 是一个 Python 库用于读写 Excel 2010 及以上版本的 xlsx/xlsm/xltx/xltm 文件。
2.1 安装 openpyxl
pip install openpyxl2.2 写入 Excel 文件
from openpyxl import Workbook# 创建工作簿
wb Workbook()
ws wb.active# 添加数据
data [[Name, Age, City],[Alice, 30, New York],[Bob, 25, Los Angeles]
]for row in data:ws.append(row)# 保存到文件
wb.save(example.xlsx)2.3 读取 Excel 文件
from openpyxl import load_workbook# 加载工作簿
wb load_workbook(example.xlsx)
ws wb.active# 读取数据
for row in ws.iter_rows(values_onlyTrue):print(row)3 使用 pandas
pandas 是 Python 数据分析的核心库提供了简单易用的数据结构和数据分析工具。
3.1 安装 pandas 和 openpyxl
pip install pandas openpyxl3.2 写入 Excel 文件
import pandas as pd# 数据
df pd.DataFrame({Name: [Alice, Bob],Age: [30, 25],City: [New York, Los Angeles]
})# 写入 Excel 文件
df.to_excel(example.xlsx, indexFalse)3.3 读取 Excel 文件
# 读取 Excel 文件
df pd.read_excel(example.xlsx)# 显示数据
print(df)4 实例演示
以下是一个 Python 示例演示如何使用 pandas 和 openpyxl 库来读取和写入 Excel 文件。这个示例将包括两个主要部分使用 pandas 进行简单的 Excel 文件读写操作以及使用 openpyxl 进行更细粒度的操作。我们将这些功能封装在一个名为 excel_example.py 的 Python 文件中。
4.1 安装所需库
在运行此脚本之前请确保已经安装了 pandas 和 openpyxl
pip install pandas openpyxl4.2 封装为excel_example.py脚本文件
import pandas as pd
from openpyxl import load_workbookdef write_excel_with_pandas(file_name, data): 使用 pandas 写入 Excel df pd.DataFrame(data)df.to_excel(file_name, indexFalse)def read_excel_with_pandas(file_name): 使用 pandas 读取 Excel df pd.read_excel(file_name)print(df)def read_excel_with_openpyxl(file_name): 使用 openpyxl 读取 Excel workbook load_workbook(filenamefile_name)sheet workbook.activefor row in sheet.iter_rows(values_onlyTrue):print(row)def main():# 文件名file_name example.xlsx# 数据data {Name: [Alice, Bob],Age: [30, 25],City: [New York, Los Angeles]}# 使用 pandas 写入 Excelwrite_excel_with_pandas(file_name, data)# 使用 pandas 读取 Excelprint(使用 pandas 读取 Excel:)read_excel_with_pandas(file_name)# 使用 openpyxl 读取 Excelprint(\n使用 openpyxl 读取 Excel:)read_excel_with_openpyxl(file_name)if __name__ __main__:main()运行以上代码后控制台输出结果 使用 pandas 读取 Excel: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles 使用 openpyxl 读取 Excel: (‘Name’, ‘Age’, ‘City’) (‘Alice’, 30, ‘New York’) (‘Bob’, 25, ‘Los Angeles’) 同时生成如下Excel文件“example.xlsx” 在这个脚本中我们定义了三个函数write_excel_with_pandas 用于写入 Excel 文件read_excel_with_pandas 和 read_excel_with_openpyxl 分别用于读取 Excel 文件。main 函数中整合了这些操作的流程。运行这个脚本将会创建一个名为 sample.xlsx 的文件并在其中写入数据然后使用两种不同的方法读取并打印出这些数据。
5 注意事项
当使用 openpyxl 时可以更加细致地控制 Excel 文件的每个方面如样式、图表等。pandas 提供了更高级的数据处理能力适合于复杂的数据分析任务。确保安装了最新版本的 openpyxl 和 pandas以便使用最新的功能和修复。
6 总结
通过本文的介绍您应该能够理解并开始在 Python 中使用 openpyxl 和 pandas 来处理 Excel 文件。无论是进行简单的数据记录还是复杂的数据分析这些工具都将是您强大的助手。 希望这篇文章能帮助您理解和掌握 Python 中处理 Excel 文件的方法。如果您有任何疑问或建议请在评论区留言让我们共同进步 作者climber1121 链接https://blog.csdn.net/climber1121 来源CSDN 版权声明本文为博主原创文章转载请附上原文出处链接和本声明。