Python自动化处理Excel与CSV数据实战指南

发布时间:2026/9/15 0:16:33
Python自动化处理Excel与CSV数据实战指南 1. 项目概述在日常办公和数据处理中Excel和CSV文件是最常见的两种数据存储格式。作为数据分析师我经常需要处理大量这类文件手动操作不仅效率低下还容易出错。Python凭借其强大的数据处理能力成为了解决这类问题的利器。通过Python脚本批量处理Excel和CSV文件可以实现数据清洗、格式转换、合并拆分等操作将原本需要数小时的手工工作缩短到几分钟内完成。本文将分享我在实际工作中总结的一套完整解决方案涵盖从基础操作到高级技巧的全流程。2. 核心工具选择2.1 必备Python库处理Excel和CSV文件主要依赖以下几个核心库openpyxl专门用于读写Excel 2010 xlsx/xlsm/xltx/xltm文件pandas提供DataFrame数据结构支持各种数据操作csvPython标准库处理CSV文件的基础模块xlrd/xlwt处理旧版Excel文件(.xls格式)安装命令pip install openpyxl pandas xlrd xlwt2.2 工具选型考量选择这些库主要基于以下考虑功能完整性覆盖新旧Excel格式和CSV文件性能表现pandas底层使用C优化处理大数据集效率高社区支持这些库都有活跃的维护和丰富的文档兼容性支持Windows/Linux/macOS多平台3. 基础操作实战3.1 读取Excel文件使用openpyxl读取Excel工作簿from openpyxl import load_workbook # 加载工作簿 wb load_workbook(data.xlsx) # 获取工作表 sheet wb[Sheet1] # 读取单元格数据 cell_value sheet[A1].value3.2 写入CSV文件使用csv模块写入数据import csv with open(output.csv, w, newline) as f: writer csv.writer(f) writer.writerow([Name, Age, City]) # 写入表头 writer.writerow([Alice, 25, New York]) # 写入数据行3.3 使用pandas处理数据pandas提供了更高级的数据操作接口import pandas as pd # 读取Excel文件 df pd.read_excel(data.xlsx) # 数据清洗 df df.dropna() # 删除空值行 df[Age] df[Age].astype(int) # 转换数据类型 # 保存为CSV df.to_csv(cleaned_data.csv, indexFalse)4. 高级批量处理技巧4.1 批量处理文件夹中的文件import os import glob # 获取所有Excel文件 excel_files glob.glob(data/*.xlsx) for file in excel_files: df pd.read_excel(file) # 处理数据... output_name os.path.splitext(file)[0] _processed.csv df.to_csv(output_name, indexFalse)4.2 多表合并操作all_data pd.DataFrame() for file in excel_files: df pd.read_excel(file) all_data pd.concat([all_data, df], ignore_indexTrue) # 保存合并结果 all_data.to_excel(combined_data.xlsx, indexFalse)4.3 条件筛选与转换# 筛选特定条件的数据 filtered df[(df[Age] 30) (df[City] Beijing)] # 添加计算列 df[Birth_Year] 2023 - df[Age] # 分组统计 group_stats df.groupby(City)[Age].mean()5. 性能优化与问题排查5.1 处理大型文件对于超大Excel文件(50MB)建议使用chunksize参数分块读取关闭不必要的格式信息考虑转换为CSV处理# 分块读取大文件 chunk_size 10000 for chunk in pd.read_excel(large_file.xlsx, chunksizechunk_size): process(chunk)5.2 常见错误处理编码问题指定正确的编码格式pd.read_csv(data.csv, encodingutf-8)日期格式明确指定日期列df[Date] pd.to_datetime(df[Date], format%Y-%m-%d)内存不足使用dtype参数优化数据类型dtypes {Age: int8, Price: float32} df pd.read_excel(data.xlsx, dtypedtypes)6. 实战案例销售数据分析假设我们需要处理一个月的销售数据从多个Excel文件中提取数据清洗异常值和缺失值计算各产品销售额生成可视化报表# 步骤1数据整合 sales_data pd.concat([pd.read_excel(f) for f in glob.glob(sales_*.xlsx)]) # 步骤2数据清洗 sales_data sales_data.dropna(subset[Product, Amount]) sales_data sales_data[sales_data[Amount] 0] # 步骤3数据分析 product_sales sales_data.groupby(Product)[Amount].sum().sort_values(ascendingFalse) # 步骤4输出结果 product_sales.to_excel(sales_report.xlsx)7. 自动化脚本开发将常用操作封装成可复用的函数def process_excel_folder(input_folder, output_folder): 批量处理文件夹中的Excel文件 if not os.path.exists(output_folder): os.makedirs(output_folder) for file in glob.glob(os.path.join(input_folder, *.xlsx)): try: df pd.read_excel(file) # 数据处理逻辑... output_file os.path.join(output_folder, os.path.basename(file)) df.to_excel(output_file, indexFalse) except Exception as e: print(f处理文件{file}时出错: {str(e)})8. 扩展应用场景8.1 与数据库交互将处理后的数据存入数据库from sqlalchemy import create_engine engine create_engine(mysqlpymysql://user:passwordlocalhost/db) df.to_sql(table_name, engine, if_existsreplace, indexFalse)8.2 生成可视化报告使用matplotlib生成图表import matplotlib.pyplot as plt df.plot(kindbar, xProduct, ySales) plt.title(Product Sales Report) plt.savefig(sales_chart.png)8.3 定时自动化任务结合Windows任务计划或Linux cron实现定时运行# Linux crontab示例 0 2 * * * /usr/bin/python3 /path/to/your/script.py9. 实用技巧与注意事项文件备份处理前先创建副本防止原始数据损坏日志记录添加日志功能跟踪处理过程异常处理使用try-except捕获可能出现的错误内存管理处理大文件时监控内存使用情况版本兼容注意不同Excel版本间的格式差异# 完善的异常处理示例 try: df pd.read_excel(data.xlsx) # 处理逻辑... except FileNotFoundError: print(文件未找到请检查路径) except PermissionError: print(没有文件访问权限) except Exception as e: print(f发生未知错误: {str(e)})10. 完整项目示例下面是一个完整的脚本示例实现了批量读取指定文件夹中的Excel文件合并相同结构的工作表执行数据清洗和转换输出处理结果和统计报告import os import glob import pandas as pd from datetime import datetime def process_excel_files(input_path, output_path): 处理Excel文件主函数 start_time datetime.now() print(f开始处理: {start_time}) # 确保输出目录存在 os.makedirs(output_path, exist_okTrue) # 获取所有Excel文件 excel_files glob.glob(os.path.join(input_path, *.xlsx)) if not excel_files: print(未找到Excel文件) return # 初始化结果DataFrame combined_data pd.DataFrame() # 处理每个文件 for file in excel_files: try: print(f正在处理: {os.path.basename(file)}) # 读取Excel文件 df pd.read_excel(file) # 数据清洗 df clean_data(df) # 合并数据 combined_data pd.concat([combined_data, df], ignore_indexTrue) except Exception as e: print(f处理文件{file}时出错: {str(e)}) continue # 生成统计报告 if not combined_data.empty: generate_report(combined_data, output_path) # 保存合并后的数据 output_file os.path.join(output_path, combined_data.xlsx) combined_data.to_excel(output_file, indexFalse) print(f结果已保存到: {output_file}) # 计算耗时 end_time datetime.now() duration end_time - start_time print(f处理完成耗时: {duration.total_seconds():.2f}秒) def clean_data(df): 数据清洗函数 # 删除空值行 df df.dropna(subset[ID, Name]) # 转换数据类型 if Amount in df.columns: df[Amount] pd.to_numeric(df[Amount], errorscoerce) # 标准化文本列 if Category in df.columns: df[Category] df[Category].str.upper().str.strip() return df def generate_report(df, output_path): 生成统计报告 report { total_records: len(df), start_date: df[Date].min(), end_date: df[Date].max(), unique_customers: df[CustomerID].nunique(), total_amount: df[Amount].sum() } # 保存报告 report_file os.path.join(output_path, report.txt) with open(report_file, w) as f: for key, value in report.items(): f.write(f{key}: {value}\n) print(f统计报告已生成: {report_file}) if __name__ __main__: input_folder input_data output_folder processed_data process_excel_files(input_folder, output_folder)在实际工作中我发现这套方法可以处理90%以上的Excel/CSV数据处理需求。对于特别复杂的场景可能需要结合具体业务逻辑进行调整。建议先从简单功能开始逐步扩展脚本的能力范围。