如何解决“'NoneType'对象不可下标”的问题?

时间:2019-07-03 14:48:10

标签: python

我试图通过python有条件地选择excel中范围内的所有单元格,但这每次都会显示。'NoneType' object is not subscriptable 有办法解决吗?

我已经尝试在str周围加上ws.cell(...),但仍然无法正常工作。

target_list = []

for i in range(1,20638):
    for j in range(1,49):
        if ws.cell(row = i, column = j).value[0:4] == "Drug":
            target_list.append(ws.cell(row = i, column = j).value[5:])
        else:
            pass 

我希望选择所有以"Drug"开头的单元格,但每次都显示'"'NoneType' object is not subscriptable'。'

1 个答案:

答案 0 :(得分:1)

只需在该行之前添加支票:

target_list = []

for i in range(1,20638):
    for j in range(1,49):
        if (ws.cell(row = i, column = j).value) is None:
            continue
        if ws.cell(row = i, column = j).value[0:4] == "Drug":
            target_list.append(ws.cell(row = i, column = j).value[5:])
        else:
            pass