我正在尝试从以下内容中提取到达时间值,这似乎是一个词典列表:
[{'arrival': {'time': 1508791028L},
'departure': {'time': 1508791028L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508790596L},
'departure': {'time': 1508790596L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508791744L},
'departure': {'time': 1508791744L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508792223L},
'departure': {'time': 1508792223L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508793450L},
'departure': {'time': 1508793450L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508792591L},
'departure': {'time': 1508792591L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508794110L},
'departure': {'time': 1508794110L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508794740L},
'departure': {'time': 1508794740L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508788421L},
'departure': {'time': 1508788421L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508788919L},
'departure': {'time': 1508788919L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508789417L},
'departure': {'time': 1508789417L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508790287L},
'departure': {'time': 1508790287L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508790347L},
'departure': {'time': 1508790347L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508791330L},
'departure': {'time': 1508791330L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508791799L},
'departure': {'time': 1508791799L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508792447L},
'departure': {'time': 1508792447L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508793300L},
'departure': {'time': 1508793300L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508793840L},
'departure': {'time': 1508793840L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508794380L},
'departure': {'time': 1508794380L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508794800L},
'departure': {'time': 1508794800L},
'schedule_relationship': 0,
'stop_id': u'D03N'},
{'arrival': {'time': 1508795220L},
'departure': {'time': 1508795220L},
'schedule_relationship': 0,
'stop_id': u'D03N'}]
然而,当我开始尝试通过简单的方式理解列表的结构时:
for i in in_dict:
print i
print "************************"
我得到了这样的输出:
[{'arrival': {'time': 1508791028L},
************************
'departure': {'time': 1508791028L},
************************
'schedule_relationship': 0,
************************
'stop_id': u'D03N'},
************************
{'arrival': {'time': 1508790596L},
************************
'departure': {'time': 1508790596L},
这个输出告诉我,列表中的元素可能不是真正的字典(例如“{'到达':{'时间':1508790596L}”似乎需要另一个“}”才能正确构建)。
我的主要问题是从这些数据中提取到达时间的最佳方法是什么?我的第二个问题是,这实际上是一个词典列表,还是只是一个与词典列表有相似之处的项目列表?
答案 0 :(得分:0)
我通过将现有字典写入文件来创建这个假字典:
with open('small_list.txt', 'wt') as out:
pprint(small_list, stream=out)
然后将small_list.txt文件导入到一个简单的python脚本中,尝试使用它,而不会影响其余的脚本。然而,正如@bradsolomon在评论中帮助我意识到的那样,在以这种方式编写文件时,我完全沉浸在结构中。这创造了诱饵字典。因此,这个问题的答案可能是“不要用stream = out"保存字典列表”。或类似的东西。