首页 > 解决方案 > 通过并行处理将 xls 批量转换为 csv

问题描述

我定期将 30 gb xls 文件批量转换为 csv。为了减少时间我应该如何在 i5 windows pc 上使用并行处理。(它目前需要我 3 小时进行 30 GB 转换)。我是新手

import glob
import pandas as pd

excel_files = glob.glob(r'filepath\*.xls') 

for excel in excel_files:

    out = excel.split('.')[0] +'.csv'


    df = pd.read_excel(excel) # if only the first sheet is needed.


    df.to_csv(out)

具有多处理功能的代码现在可以工作,但比上面的代码花费更多时间(3 倍!)。阅读并发现可能是由于输入输出操作。我们可以加快速度吗?

import multiprocessing 
import glob
import pandas as pd
excel_files = glob.glob(r'C:\Test\*.xls') # the path where your files are
counter= 0
def multi():
    for excel in excel_files:
    out = excel.split('.')[0] +'.csv'

    df = pd.read_excel(excel) # if only the first sheet is needed.

    df.to_csv(out)

    return

if __name__ =='__main__':
#freeze_support()
    queue = multiprocessing.Queue()
    #num_cores=multiprocessing.cpu_count()
    #print(num_cores)
    pool = multiprocessing.Pool()

    result= pool.map(multi,excel_files,8)

标签: pythonpython-3.xparallel-processingbatch-processing

解决方案


推荐阅读