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

问题描述 投票:0回答:1

我们希望使用以下代码从实体内的列表中提取停靠站到达时间和出发时间。但是,我不断收到错误。

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#

python dataframe get protocol-buffers gtfs
1个回答
0
投票

以下是如何从这本字典中获取到达和出发时间的示例:

dct = {
    "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": {},
            },
        ],
    },
}

for d in dct["tripUpdate"]["stopTimeUpdate"]:
    arrival = d["arrival"]["time"]
    departure = d["departure"]["time"]

    print(f"{arrival=} {departure=}")

打印:

arrival='1710344086' departure='1710344086'
arrival='1710343956' departure='1710343956'
arrival='1710343995' departure='1710343995'
© www.soinside.com 2019 - 2024. All rights reserved.