嵌套的 if-else 语句未返回正确的值

Nested if-else statements not returning correct value

我正在尝试打印 excel 文件中是否包含列值。

起初,我使用了一个 if 语句,然后是两个 elif,最后是一个 else。然后我将 elifs 更改为 if-else 语句,因为我认为这是问题所在,但我仍然得到不正确的值。

if 'Strip Circuit ID' in ws.columns:
    print('Contains Strip Circuit ID')
    #col6
    cell = ws.cell(row=1, column=6)
    print(cell.value)
else:
    if 'STRIP_EC_CIRCUIT_ID' in ws.columns:
        print('Contains STRIP_EC_CIRCUIT_ID')
        #col9
        cell = ws.cell(row=1, column=9)
        print(cell.value)
    else:
        if 'Circuit ID' in ws.columns:
            print('Contains Circuit ID')
            #col6
            cell = ws.cell(row=1, column=6)
            print(cell.value)
        else:
            print('NONE')

我希望当文件有 'Strip Circuit ID'、'STRIP_EC_CIRCUIT_ID' 或 'Circuit ID' 时,它将 return if-else 语句中的值,但是程序只是 returning 'NONE'.

ws.columns returns 列生成器,每个列都包含单元格,因此检查没有任何意义。您必须检查每一列中每个单元格的值。

for col in ws.columns:
    for cell in col:
        if "Circuit ID" in cell.value:
            print(cell.coordinate)