簡體   English   中英

如何解決“'NoneType' 對象不可下標”的問題?

[英]How to fix the problem of "'NoneType' object is not subscriptable."?

我試圖通過python有條件地選擇excel范圍內的所有單元格,但這每次都會顯示。 'NoneType' object is not subscriptable有沒有辦法修復它?

我已經嘗試在ws.cell(...)周圍放置一個str ,但它仍然不起作用。

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'

只需在該行之前添加一個檢查:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM