如何从Google.protobuf提取特定字段(或仅此转换为字符串)

发布于 2025-02-03 03:54:00 字数 990 浏览 1 评论 0 原文

我从API中获得了有关我城市中电车和公共汽车的实时数据,这种类型是:

print(type(feed.entity))
<class 'google.protobuf.pyext._message.RepeatedCompositeContainer'>

看起来像这样(转换为字符串看起来相同):

[id: "4312"
vehicle {
  trip {
    trip_id: "\"1_740096^K"
    schedule_relationship: SCHEDULED
    route_id: "488"
  }
  position {
    latitude: 52.335411071777344
    longitude: 17.164840698242188
    speed: 14.4399995803833
  }
  current_stop_sequence: 2
  timestamp: 1654021682
  vehicle {
    id: "4312"
    label: "488/12"
  }
}
, id: "4318"
vehicle {
  trip {
    trip_id: "\"1_740217^G"
    schedule_relationship: SCHEDULED
    route_id: "432"
  }
  position {
    latitude: 52.39680862426758
    longitude: 16.94964027404785
    speed: 0.0
  }
  current_stop_sequence: 0
  current_status: INCOMING_AT
  timestamp: 1654021677
  vehicle {
    id: "4318"
    label: "432/8"
  }
}
,

我的目的是获取每辆车的ID,Route_ID,纬度和经度(然后)使用熊猫,将此点放入叶叶中)。我想知道什么是从原始格式或字符串中提取此方法的最佳方法。

I've got live data about trams and buses in my city from API, the type is:

print(type(feed.entity))
<class 'google.protobuf.pyext._message.RepeatedCompositeContainer'>

It looks like this (converted to string looks the same):

[id: "4312"
vehicle {
  trip {
    trip_id: "\"1_740096^K"
    schedule_relationship: SCHEDULED
    route_id: "488"
  }
  position {
    latitude: 52.335411071777344
    longitude: 17.164840698242188
    speed: 14.4399995803833
  }
  current_stop_sequence: 2
  timestamp: 1654021682
  vehicle {
    id: "4312"
    label: "488/12"
  }
}
, id: "4318"
vehicle {
  trip {
    trip_id: "\"1_740217^G"
    schedule_relationship: SCHEDULED
    route_id: "432"
  }
  position {
    latitude: 52.39680862426758
    longitude: 16.94964027404785
    speed: 0.0
  }
  current_stop_sequence: 0
  current_status: INCOMING_AT
  timestamp: 1654021677
  vehicle {
    id: "4318"
    label: "432/8"
  }
}
,

My purpose is to get id, route_id, latitude and longitude of each vehicle (then use pandas and put this points into a map in folium). I'd like to know what's the best way to extract this either from an original format or a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋情葬爱 2025-02-10 03:54:00

您可以使用 protobuf ,然后访问所需的字段。

You can convert the message to dictionary using MessageToDict from protobuf, then access the fields that you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文