简体   繁体   English

使用python将大型xlsx拆分为多个csv文件

[英]split large xlsx to multiple csv file using python

I am trying to generate multiple csv file by splitting single large xls file using pandas, but only generates a single small file, rest files are not getting generated.我试图通过使用 Pandas 拆分单个大 xls 文件来生成多个 csv 文件,但只生成一个小文件,其余文件没有生成。 Please find the below sample function.请找到以下示例函数。

def xls_split_to_csv(sourceFilePath):
curr_date = datetime.now().strftime("%d-%m-%Y")
curr_time = datetime.now().strftime("%H-%M-%S")
try:
    logMessage("--------------------------------------------------------")
    logMessage(f'######### Split engine started - {curr_date}  ##########')
    logMessage("--------------------------------------------------------")
    chunk_size = config_params['split_size']
    batch = 0
    df = pd.read_excel(sourceFilePath)
    o_filename = f'file_{curr_date}_{curr_time}_{batch + 1}.csv'
    file_count = math.ceil(len(df) / chunk_size)
    for chunk in np.array_split(df, file_count):
        logMessage(f'Splitting file ----> ({batch + 1} of {file_count})')
        output_path = os.path.join('../TruncatedFile', o_filename)
        chunk.to_csv(output_path, index=False, header=True)
        batch += 1

except ZeroDivisionError as zeroEx:
    logError("Exception: ")
except Exception as ex:
    logError("Exception: ")

Move the line移动线

o_filename = f'file_{curr_date}_{curr_time}_{batch + 1}.csv'

into your for loop;进入你的for循环; at the moment you never change the output file name.目前您永远不会更改输出文件名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM