我有一个包含多张工作表的 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/