无法加载大型Excel文件(Python)

时间:2018-04-20 11:54:35

标签: python excel xlsx openpyxl

我正在尝试使用.xlsx加载大型OpenPyXL文件,而加载80 MB .xlsx文件时,我的整个8 GB内存已满了

enter image description here

Excel文件有4张,有80万行。

from openpyxl import load_workbook
wb = load_workbook('Record.xlsx')

def process(ws):
    '''
    Read all rows of a worksheet
    '''
    data = []
    for a, c, f, k in zip(ws['A'], ws['C'], ws['F'], ws['K']):
        data.append([a, c, f, k])

    return data

ws1 = wb.worksheets[0] # Sheet 1
data1 = process(ws1)
ws2 = wb.worksheets[1] # Sheet 2
data2 = process(ws2)
ws3 = wb.worksheets[2] # Sheet 3
data3 = process(ws3)
ws4 = wb.worksheets[3] # Sheet 4
data4 = process(ws4)

为什么在加载80 MB的excel文件时8 GB的内存是不够的?

1 个答案:

答案 0 :(得分:2)

尝试使用PatGroup Variable Value StudyQuarter A Patientdays 100 1 B ExposedDays 80 1 A ExposedDays 40 1 A Patients 40 1 C ExposedDays 10 1 C PatientDays 90 1 A Patientdays 20 2 B ExposedDays 90 2 参数来使用只读模式,如Serilog所述。