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

标签 python python-3.x excel pandas jupyter-notebook

我有一个包含多张工作表的 Excel 文件。我想将这些工作表转换为单独的 CSV 文件。

我试过这段代码,得到了一个有序的表格字典。现在我需要一步将它们保存为 CSV 文件,而不必在单独的步骤中手动保存每个文件

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

最佳答案

您可以使用 to_csv 将数据帧保存为 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')

关于python - 使用 Jupyter 笔记本将具有多个工作表的 Excel 文件转换为多个 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63291259/

相关文章:

Excel VBA根据列表框选择填充列表框

excel - Java 开源库从 excel 中生成数据透视表

python - 将 where 与 pandas 和分类列一起使用时出错

python - 为什么 datetime.datetime.now().timestamp() 和 datetime.datetime.utcnow().timestamp() 之间存在差异?

python - 将 pandas DataFrame 转换为 excel 工作表时删除标题中的默认格式

python - Tkinter 文本小部件变形列大小

python - 比较多个行值

Python:带有 [< >, <>...] 的列表

python - 我什么时候应该使用 iter 函数?

python - 在python中设置文档的行号并获取数据