简体   繁体   中英

OpenPyXL - How to skip row if cell.value is None

I've read plenty of answers here regarding empty rows, but somehow my attempts to adapt those solutions to my script has failed.

I'm reading sheets in excel files using OpenPyXL and loading it's part which is to be processed to DataFrame (first read each row to list of lists and then convert it to DataFrame). The thing is that I'm looking for elegant solution to skip row if the cell.value of the first cell is None

I iterate through rows with the below code:

for row in ws.iter_rows(min_col=adres[0], min_row=adres[1], max_col=adres[2], max_row=adres[3]):
    data_rows.append([cell.value for cell in row]) 

Thank you, the solution is so simple I'm ashamed I was even asking :)

Here it is working nicely:

for row in ws.iter_rows(min_col=adres[0], min_row=adres[1], max_col=adres[2], max_row=adres[3]):
    if row[0].value is not None:
        data_rows.append([cell.value for cell in row])
    else: continue

The following is probably something like what you want:

def skip_empty_rows(ws):
    for row in ws.values:
        if row[0] is None:
           continue
        yield row

df = pd.DataFrame((skip_empty_rows(ws))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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