1. 程式人生 > >python報錯 TypeError: string indices must be integers

python報錯 TypeError: string indices must be integers

info span 判斷 integer lte split error ret str

所以在讀取字典的時候,最好先判斷類型,然後再查看它是否已經有這樣的屬性: type(mydict) == type({}) #檢查不是字典 如果是字典,再看看有沒有這樣的屬性:mydict.has_key(‘mykey‘) 1、 看看變量是否是字典 2、檢查字典是否有對應的key值
    if like in condition:
        cond_str1 = condition.split(like)[0].strip()
        cond_str2 = condition.split(like)[1].strip()
        
print(staff_info) ret = filter(lambda x:cond_str2 in str(x[cond_str1]), staff_info) # 這裏字典寫錯

技術分享圖片

python報錯 TypeError: string indices must be integers