[英]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.