简体   繁体   English

OpenPyXL-如果cell.value为None则如何跳过行

[英]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). 我正在使用OpenPyXL在excel文件中读取工作表,并将其处理的部分加载到DataFrame(首先将每一行读取到列表列表,然后将其转换为DataFrame)。 The thing is that I'm looking for elegant solution to skip row if the cell.value of the first cell is None 事实是,如果第一个单元格的cell.value为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 :) 谢谢,解决方案是如此简单,我很m愧,甚至还问过:)

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))

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

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