Python 从复杂的诱饵/假字典列表中提取值

Python 从复杂的诱饵/假字典列表中提取值,python,list,dictionary,Python,List,Dictionary,我正在尝试从下面的字典列表中提取到达时间值: [{'arrival': {'time': 1508791028L}, 'departure': {'time': 1508791028L}, 'schedule_relationship': 0, 'stop_id': u'D03N'}, {'arrival': {'time': 1508790596L}, 'departure': {'time': 1508790596L}, 'schedule_relationship':

我正在尝试从下面的字典列表中提取到达时间值:

[{'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},
该输出向我表明,列表中的元素可能不是真正的字典(例如“{'arrival':{'time':1508790596L}”似乎需要另一个“}”来正确构造)


我的主要问题是,从这些数据中提取到达时间的最佳方法是什么?我的第二个问题是,这实际上是一个字典列表,还是仅仅是一个碰巧与字典列表有相似之处的项目列表?

我通过将现有字典写入一个包含以下内容的文件创建了这个假字典:

with open('small_list.txt', 'wt') as out:
     pprint(small_list, stream=out)

然后将小的_list.txt文件导入到一个简单的python脚本中,尝试使用它,而不让脚本的其余部分碍事。然而,@bradsolomon在评论中帮助我意识到,以这种方式编写文件完全破坏了结构。这就创建了诱饵词典。因此,这个问题的答案可能是“不要用stream=out保存字典列表”或类似的东西。

是的,您的“dict”是一个行列表。你可能会逐行阅读文件,而不是像你应该做的那样使用
json
ast.literal\u eval
。这看起来确实像是json的“记录”方向,只是我们可以使用更多的信息来了解你从哪里获得它,它是如何存储的,等等。。。你把它作为变量吗?作为文本文件?它看起来很像json。我问这个问题是因为不清楚1508791028L、1508791028L的格式是如何的,没有使用
orient='records'
的引号。