文章内容
2020/10/29 11:06:10,作 者: 黄兵
TypeError: 'Menu' object is not subscriptable
在使用Python循环遍历字典的时候出现了如下错误:
TypeError: 'Menu' object is not subscriptable
错误代码:
temp_list = [] for item in get_parent_menu: if item.section is None: temp_list.append({'title': item['title']}) else: temp_list.append({'section': item['section']}) print(temp_list)
这里获取的是一个list列,结果如下:
出错原因:
没搞明白☺😅
解决方案:
修改代码:
temp_list = [] for item in get_parent_menu: if item.section is None: temp_list.append({'title': item.title, 'root': item.title, 'icon': item.icon, 'page': item.page, 'svg': item.svg, 'bullet': item.bullet}) else: temp_list.append({'section': item.section}) print(temp_list)
黄兵个人博客原创。
转载请注明出处:黄兵个人博客 - TypeError: 'Menu' object is not subscriptable
评论列表