使用get()函数读取GTFS tripupdates.pb实时数据时出错

1 投票
1 回答
39 浏览
提问于 2025-04-14 15:44

我们想要从实体中的列表里提取到到达时间和出发时间,使用了以下代码。不过,我遇到了重复的错误。

dict_obj['entity'][0]

Gives the following output


{'id': '8800314',
 'tripUpdate': {'trip': {'tripId': '8800314',
   'startTime': '11:30:00',
   'startDate': '20240313',
   'routeId': '20832'},
  'stopTimeUpdate': [{'stopSequence': 1,
    'arrival': {'time': '1710344086'},
    'departure': {'time': '1710344086'},
    'stopId': '86900',
    'stopTimeProperties': {}},
   {'stopSequence': 2,
    'arrival': {'time': '1710343956'},
    'departure': {'time': '1710343956'},
    'stopId': '86024',
    'stopTimeProperties': {}},
   {'stopSequence': 3,
    'arrival': {'time': '1710343995'},
    'departure': {'time': '1710343995'},
    'stopId': '86560',
    'stopTimeProperties': {}},
   {'stopSequence': 4,

我们想提取到达时间:

#for trip updates

collector1 = []
counter1=0
for block1 in dict_obj1['entity']:
    counter1 += 1
    row = OrderedDict()
    row['stop_AT'] = block1.get('tripUpdate').get('stopTimeUpdate')[0].get('arrival').get('time')
    row['stop_DT'] = block1.get('tripUpdate').get('stopTimeUpdate')[0].get('departure').get('time')
    collector1.append(row)
df1 = pd.DataFrame(collector1)        

Error:

AttributeError: 'list' object has no attribute 'get'

代码来源:

https://nbviewer.org/url/nikhilvj.co.in/files/gtfsrt/locations.ipynb#

1 个回答

0

这里有一个例子,教你如何从这个字典中获取到到达和离开的时间:

dict_obj1 = {
    "entity": [
        {
            "id": "8800314",
            "tripUpdate": {
                "trip": {
                    "tripId": "8800314",
                    "startTime": "11:30:00",
                    "startDate": "20240313",
                    "routeId": "20832",
                },
                "stopTimeUpdate": [
                    {
                        "stopSequence": 1,
                        "arrival": {"time": "1710344086"},
                        "departure": {"time": "1710344086"},
                        "stopId": "86900",
                        "stopTimeProperties": {},
                    },
                    {
                        "stopSequence": 2,
                        "arrival": {"time": "1710343956"},
                        "departure": {"time": "1710343956"},
                        "stopId": "86024",
                        "stopTimeProperties": {},
                    },
                    {
                        "stopSequence": 3,
                        "arrival": {"time": "1710343995"},
                        "departure": {"time": "1710343995"},
                        "stopId": "86560",
                        "stopTimeProperties": {},
                    },
                ],
            },
        }
    ]
}

out = []
for block in dict_obj1["entity"]:
    for d in block["tripUpdate"]["stopTimeUpdate"]:
        arrival = d.get("arrival", {}).get("time")
        departure = d.get("departure", {}).get("time")
        out.append({"arrival": arrival, "departure": departure})

df = pd.DataFrame(out)
print(df)

输出结果是:

      arrival   departure
0  1710344086  1710344086
1  1710343956  1710343956
2  1710343995  1710343995

撰写回答