【发布时间】:2016-05-21 07:56:29
【问题描述】:
if story['epic']['id'] == epic['id']:
TypeError: 'NoneType' object is not subscriptable
代码:
for epic in epics:
if epic['archived'] is False:
print(epic['short_name'], epic['summary'], epic['story_count'])
for story in stories:
if story['epic']['id'] == epic['id']:
print(story['summary'], story['cell']['label'])
我可能搞错了,但仍在尝试学习 json/dict 迭代以及如何在多个迭代之间进行比较。
我正在尝试从将 ID 与同一史诗 ID 匹配的故事中检索数据。我知道story['epic'] 下包含['id'] 和['local_id'],但显然我无法将其与非故事字典数据进行比较?
注意:story['epic']['id'] 的值可以为空。
【问题讨论】:
-
错误信息告诉你
story、story['epic']或epic等于None;你可以从找出它是什么开始。 -
你可以通过在有问题的
if之前插入print(epic)和print(story)来找到问题 -
story或store['epic']是None,因此表达式的下一个[...]部分应用于该对象而不是字典。您必须弄清楚是哪一个以及如何处理。 -
哦,所以实际的列表可能在故事或故事['epic'] 中没有。好的,这是一个好的开始。
标签: python if-statement for-loop dictionary