json 如何过滤并存储所有具有特定值标签?

gzszwxb4  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(107)

我有一个很长的json文件,想从中提取一些信息。这是其中的一些行:

{
      "type": "node",
      "id": 4000703812,
      "lat": -37.8664035,
      "lon": 145.029337,
      "tags": {
        "name": "Malvern",
        "name:zh": "墨尔文",
        "network": "PTV – Metropolitan Trains",
        "operator": "Metro Trains Melbourne",
        "public_transport": "stop_position",
        "ref": "MAL",
        "ref:ptv": "19944",
        "train": "yes",
        "website": "https://ptv.vic.gov.au/stop/1118",
        "wikidata": "Q6745096",
        "wikipedia": "en:Malvern railway station, Melbourne"
      }
    },
    {
      "type": "node",
      "id": 4000703813,
      "lat": -37.8664239,
      "lon": 145.0293099,
      "tags": {
        "name": "Malvern",
        "name:zh": "墨尔文",
        "network": "PTV – Metropolitan Trains",
        "operator": "Metro Trains Melbourne",
        "public_transport": "stop_position",
        "ref": "MAL",
        "ref:ptv": "19944",
        "train": "yes",
        "website": "https://ptv.vic.gov.au/stop/1118",
        "wikidata": "Q6745096",
        "wikipedia": "en:Malvern railway station, Melbourne"
      }
    },
   {
      "type": "way",
      "id": 113543936,
      "center": {
        "lat": -37.7974032,
        "lon": 144.9674811
      },
      "nodes": [
        8864592602,
        8864592606,
        8864592599,
        8864592600,
        8864592601,
        1287222216
      ],
      "tags": {
        "area": "yes",
        "bus": "yes",
        "departures_board": "timetable",
        "name": "Elgin Street/Lygon Street",
        "network": "PTV - Metropolitan Buses",
        "operator": "Transdev",
        "public_transport": "platform",
        "route_ref": "200;207",
        "source": "bing",
        "surface": "asphalt",
        "tactile_paving": "no"
      }
    },
   {
      "type": "way",
      "id": 45478351,
      "center": {
        "lat": -37.7784042,
        "lon": 145.0313655
      },
      "nodes": [
        579375498,
        5515852169,
        579375499
      ],
      "tags": {
        "public_transport": "platform",
        "railway": "platform"
      }
    },
   {
      "type": "way",
      "id": 45478438,
      "center": {
        "lat": -37.7689219,
        "lon": 145.0454472
      },
      "nodes": [
        579377100,
        579377102
      ],
      "tags": {
        "bench": "yes",
        "bin": "yes",
        "lit": "yes",
        "public_transport": "platform",
        "railway": "platform",
        "ref": "1",
        "shelter": "yes",
        "tactile_paving": "yes"
      }
    },
    {
      "type": "way",
      "id": 37892868,
      "center": {
        "lat": -37.8188306,
        "lon": 145.2145215
      },
      "nodes": [
        444681241,
        444681237,
        444681241
      ],
      "tags": {
        "area": "yes",
        "departures_board": "realtime",
        "foot": "yes",
        "layer": "-1",
        "network": "PTV – Metropolitan Trains",
        "operator": "Metro Trains Melbourne",
        "public_transport": "platform",
        "railway": "platform",
        "ref": "1",
        "surface": "asphalt",
        "train": "yes"
      }
    },

我如何存储所有具有tage "type": "way"的示例,然后进一步将它们分离出来并将它们存储为字典或python列表。我想有一个Bus,Trains,Trams的Three字典,每个字典包含具有bus,train或tram的方式的id以及所有节点的值。例如,在上面的例子中,对于bus,它将是:Busses["id: 113543936"] = [ 8864592602,8864592606,8864592599,8864592600,8864592601,1287222216]

8iwquhpp

8iwquhpp1#

只需循环遍历JSON的所有对象并填充巴士,电车和火车的字典。您还可以根据变量更改类型过滤。

import json

data = json.loads('{"version": 0.6, "generator": "Overpass API 0.7.59.4 36d058c8", "osm3s": { "timestamp_osm_base": "2023-03-29T08:14:20Z", "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL." }, "elements": [{"type":"node","id":4000703812,"lat":-37.8664035,"lon":145.029337,"tags":{"name":"Malvern","name:zh":"\u58a8\u5c14\u6587","network":"PTV \u2013 Metropolitan Trains","operator":"Metro Trains Melbourne","public_transport":"stop_position","ref":"MAL","ref:ptv":"19944","train":"yes","website":"https:\/\/ptv.vic.gov.au\/stop\/1118","wikidata":"Q6745096","wikipedia":"en:Malvern railway station, Melbourne"}},{"type":"node","id":4000703813,"lat":-37.8664239,"lon":145.0293099,"tags":{"name":"Malvern","name:zh":"\u58a8\u5c14\u6587","network":"PTV \u2013 Metropolitan Trains","operator":"Metro Trains Melbourne","public_transport":"stop_position","ref":"MAL","ref:ptv":"19944","train":"yes","website":"https:\/\/ptv.vic.gov.au\/stop\/1118","wikidata":"Q6745096","wikipedia":"en:Malvern railway station, Melbourne"}},{"type":"way","id":113543936,"center":{"lat":-37.7974032,"lon":144.9674811},"nodes":[8864592602,8864592606,8864592599,8864592600,8864592601,1287222216],"tags":{"area":"yes","bus":"yes","departures_board":"timetable","name":"Elgin Street\/Lygon Street","network":"PTV - Metropolitan Buses","operator":"Transdev","public_transport":"platform","route_ref":"200;207","source":"bing","surface":"asphalt","tactile_paving":"no"}},{"type":"way","id":45478351,"center":{"lat":-37.7784042,"lon":145.0313655},"nodes":[579375498,5515852169,579375499],"tags":{"public_transport":"platform","railway":"platform"}},{"type":"way","id":45478438,"center":{"lat":-37.7689219,"lon":145.0454472},"nodes":[579377100,579377102],"tags":{"bench":"yes","bin":"yes","lit":"yes","public_transport":"platform","railway":"platform","ref":"1","shelter":"yes","tactile_paving":"yes"}},{"type":"way","id":37892868,"center":{"lat":-37.8188306,"lon":145.2145215},"nodes":[444681241,444681237,444681241],"tags":{"area":"yes","departures_board":"realtime","foot":"yes","layer":"-1","network":"PTV \u2013 Metropolitan Trains","operator":"Metro Trains Melbourne","public_transport":"platform","railway":"platform","ref":"1","surface":"asphalt","train":"yes"}}]}')

# Initialize empty dictionaries for buses, trains, and trams
Buses = {}
Trains = {}
Trams = {}

elementsData = data["elements"]
# Loop through all the items in the data
for item in elementsData:
    # Check if the item is of type "way"
    if item['type'] == 'way':
        # Check if the item has the tag "public_transport" with value "platform"
        tags = item['tags']
        if 'public_transport' in tags and tags['public_transport'] == 'platform':
            # Check the value of the "network" tag to determine if it's a bus, train, or tram
            if 'network' in tags:
                network = tags['network']
                if 'bus' in network.lower():
                    # This is a bus platform
                    Buses['id: ' + str(item['id'])] = item['nodes']
                elif 'train' in network.lower():
                    # This is a train platform
                    Trains['id: ' + str(item['id'])] = item['nodes']
                elif 'tram' in network.lower():
                    # This is a tram platform
                    Trams['id: ' + str(item['id'])] = item['nodes']

print(Buses)

相关问题