0
我有一個vehicle['estimatedCalls']['estimatedCall']
列表包含以下項目:迭代通過Python列表
[
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24684:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24683:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24680:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24687:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24686:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
},
{
u"originDisplay": [],
u"destinationDisplay": [],
u"stopPointRef": {
u"value": "STIF:StopPoint:Q:24685:"
},
u"expectedDepartureTime": "2017-03-17T19:00:00.000Z",
u"stopPointName": [],
u"arrivalOperatorRefs": []
}
]
我想通過每個stopPointRef
迭代,expectedDepartureTime
夫婦(和不在座的,有時expectedArrivalTime
,aimedDepartureTime
和aimedArrivalTime
存在時),以檢索它們的值(爲stopPointRef
,價值不value
但第二項(與STIF:StopPoint:Q:
開始)
這裏是我當前的代碼:
for call in vehicle['estimatedCalls']['estimatedCall']:
stoptime = ent.trip_update.stop_time_update.add()
for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']):
stoptime.stop_id = vehicle['estimatedCalls']['estimatedCall']['stopPointRef']['value']
stoptime.arrival_time = call['expectedArrivalTime']
stoptime.departure_time = call['expectedDepartureTime']
的 「for」 循環似乎正常工作(print vehicle['estimatedCalls']['estimatedCall']
返回正確的列表)
但是當試圖通過每個stopPointRef
,expectedDepartureTime
組進行迭代,用:
for j in len(vehicle['estimatedCalls']['estimatedCall']['stopPointRef']):
我出現以下錯誤:TypeError: list indices must be integers, not str
您能否幫我解決這個問題並找到適當的代碼來執行該操作?謝謝你的幫助!
'len'返回一個整數,所以你寫的東西歸結爲類似'在10' j,它並沒有真正任何意義。有時候,一個人使用'範圍內的j(len(...))',但這看起來並不是你想要的。 – fuglede
this「vehicle ['estimatedCalls'] ['estimatedCall']」是一個不是字典的列表..您必須將它稱爲vehicle ['estimatedCalls'] ['estimatedCall'] [0] ['stopPointRef'] – repzero