简体   繁体   English

循环通过 excel 工作表并根据条件将每个工作表保存到 csv

[英]Loop through excel sheets and save each sheet into a csv based on a condition

I have an excel file that has multiple sheets.我有一个包含多张纸的 excel 文件。 I would like to iterate through each sheet and check against a string to either read the file after 0 rows or 4 rows.我想遍历每张纸并检查字符串以在 0 行或 4 行之后读取文件。 (As some of the sheets datasets start after the first 4 rows) After the sheet gets read I want to save the file as a csv. (因为一些工作表数据集在前 4 行之后开始)在工作表被读取后,我想将文件保存为 csv。

This is my code so far, but I am not sure if I am doing the loop correctly.到目前为止,这是我的代码,但我不确定我是否正确地执行了循环。

import pandas as pd


def converToCsv(excel_file): 
    df = pd.read_excel(excel_file, sheet_name = None)
    
    for sheets in df.items():
        if sheets[df.items()] == 'Shipment':
                  newdf = pd.read_excel(excel_file, sheet_name = sheets[df.items(), header = 4]
                  newdf.to_csv('path', decimal = ',', index = False)
        else:
                  newdf = pd.read_excel(excel_file, sheet_name = sheets[df.items(), header = 0]
                  newdf.to_csv('path', decimal = ',', index = False)


There are several things not working with the snippet you posted:有几件事不适用于您发布的代码段:

  • sheets[df.items()] is not valid. sheets[df.items()]无效。 df.items() returns a dict like: {<sheet_name>: <sheet_content>} , so you can not use it as an index df.items()返回一个类似的字典: {<sheet_name>: <sheet_content>} ,所以你不能将它用作索引
  • missing parenthesis and misplacement of square brackets缺少括号和方括号错位
  • admitting that the loop worked, you are always saving the data to the same file path , doing so you are overwriting the previously saved sheet to csv on each loop turn.承认循环工作,您总是将数据保存到相同的文件path ,这样做您将在每个循环转弯时将先前保存的工作表覆盖到 csv。

Did you try running this code before posting it?您是否在发布之前尝试运行此代码?

You could do something along those lines:你可以按照这些思路做一些事情:

import pandas as pd


def converToCsv(excel_file): 
    workbook = pd.read_excel(excel_file, sheet_name = None)
    
    for sheet_name in workbook.keys():
        header = 0
        if sheet_name == 'Shipment':
            header = 4    
        newdf = pd.read_excel(excel_file, sheet_name = sheet_name, header=header)
        # TODO: handle the case where sheet name is not a valid file name
        newdf.to_csv(f"{sheet_name}.csv", decimal = ',', index = False)

converToCsv("test.xlsx")

暂无
暂无

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

相关问题 如何将 for 循环生成的每个数据帧保存到同一个 Excel 工作表,但不同的工作表? - How to save each dataframe produced by a for loop to the same excel sheet, but different sheets? 如何遍历 excel 工作表并使用 python 在每张工作表上执行相同的任务 - How to loop through excel sheets and perform same task on each sheet using python Python:循环通过 excel 表并写入 csv - Python: loop through excel sheets and write to csv 如何遍历每个Excel工作表名称 - how to loop through each excel sheet name 基于两个条件子集数据集,将每个数据帧保存到 .csv 文件中,遍历每个文件并绘制图形 - Subsetting Dataset based on two condition, Save each dataframe into a .csv file, Iterate through each file and Plot figures 使用pandas在excel中创建多个工作表以循环工作表名称 - Create multiple sheets in excel using pandas to loop through sheet names 使用 pandas 将 2 个数据帧保存到 excel 中的工作表 1 和 2,但要保持工作表 2 与 excel 本身一样吗? - Save 2 dataframes to sheets 1 and 2 in excel with pandas but to keep sheet2 as it is in excel itself? 如何将每个表保存在单独的 excel 表中 - How to save each table in a separate excel sheet 在python中循环浏览数据框列表,并将每个df放入不同的Excel工作表中 - loop through a list of dataframes in python and wirte each df into different excel sheets 使用 Python/Pandas 提取与每个城市相关的数据,并使用循环或 function 保存在单独的 excel 表中 - Using Python/Pandas to extract data associated with each of the cities and save in separate excel sheet using loop or function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM