我有这样一个需求,读取excel中的内容,每行以字典的格式保存在一个列表中,然后遍历这个列表时要判断字典中第二个字段的内容是否为空,研究了很久,最后终于找到了一个可行的方法。

保存的数据格式

l = [
    {"A":"a1","B":"b1","C":"c1"},
    {"A":"a2","B":None,"C":"c2"},
    {"A":"a3","B":"b3","C":"c3"},
    {"A":"a4","B":"b4","C":"c4"},
]

代码如下:

l = [
    {"A":"a1","B":"b1","C":"c1"},
    {"A":"a2","B":None,"C":"c2"},
    {"A":"a3","B":"b3","C":"c3"},
    {"A":"a4","B":"b4","C":"c4"},
]

for i in l:
    d = [elem for elem in i.values()][1]
    if d:
        print(d)
最后修改:2021 年 07 月 16 日 04 : 16 PM
如果觉得我的文章对你有用,请随意赞赏