简体   繁体   English

使用 Jupyter 笔记本将具有多张工作表的 Excel 文件转换为多个 csv 文件

[英]Converting Excel file with multiple sheets into multiple csv files using Jupyter notebook

I have an excel file that contains multiple sheets.我有一个包含多张纸的 excel 文件。 I want to convert these sheets into separate CSV files.我想将这些工作表转换为单独的 CSV 文件。

I tried this code and got an ordered dictionary of the sheets.我尝试了这段代码并得到了一张有序的工作表字典。 Now I need to save them as CSV files in one step, instead of having to save each one manually in a separate step现在我需要一步将它们保存为 CSV 文件,而不必在单独的步骤中手动保存每个文件

xls = pd.ExcelFile('file.xlsx')
sheets = {}
for sheet_name in xls.sheet_names:
    sheets[sheet_name] = xls.parse(sheet_name)

You can use to_csv to save dataframe as csv file:您可以使用to_csv将 dataframe 保存为 csv 文件:

# I prefer reading excel with pd.read_excel
# passing `sheet_name=None` returns a dictionary 
# with the form {sheet_name: dataframe}
data = pd.read_excel('file.xlsx', sheet_name=None)

# loop through the dictionary and save csv
for sheet_name, df in data.items():
    df.to_csv(f'{sheet_name}.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 借助 jupyter notebook 将多个 csv 文件合并为一个新的 csv 文件 - Merge multiple csv files into a new one csv file with the help of jupyter notebook 使用 Python 将大型 CSV 文件拆分为单个 Excel 中的多个工作表 - Splitting Large CSV file into multiple sheets in a single Excel using Python Jupyter Notebook 指定目录路径以连接多个.csv 文件 - Jupyter Notebook specify path to directory for concatenation of multiple .csv files 尝试使用 python 在 Jupyter Notebook 中读取多个.csv 文件时出错 - Error when trying to read multiple .csv files in Jupyter Notebook using python 使用 Python 将大型 CSV 文件转换为多个 JSON 文件 - Converting a large CSV file to multiple JSON files using Python 如何使用 Python 将多个 CSV 文件合并为一个具有不同工作表的 Excel 文件 - How do I combine multiple CSV files into one excel files with different sheets using Python 使用 Python 将多个 Excel 文件作为单独的工作表 - Multiple Excel Files as Separate Sheets Using Python 如何将excel工作簿中的多个工作表转换为python中的csv个文件 - How to convert multiple sheets in an excel workbook to csv files in python 在 jupyter 笔记本中运行多个 .py 文件 - Run multiple .py files in a jupyter notebook 使用 pandas 读取多个 excel 文件中的多个工作表 - Read multiple sheets in multiple excel files using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM