python元素树在xmlcod中查找子节点

2024-09-27 00:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

{
  "incidents": [
    {
      "id": "48348840",
      "type": 3,
      "severity": 2,
      "eventCode": 214,
      "lat": 39.742012,
      "lng": -105.014618,
      "startTime": "2017-06-30T10:05:00",
      "endTime": "2017-06-30T12:19:38",
      "impacting": true,
      "shortDesc": "I-25 N/B: delays of four minutes between Exits 207B,208 and Exits 210A,210B",
      "fullDesc": "Delays of four minutes on I-25 Northbound between Exits 207B,208 US-85 Santa Fe Dr and Exits 210A,210B I-70 Bus Colfax Ave.",
      "delayFromFreeFlow": 3.9700000286102295,
      "delayFromTypical": 1.5199999809265137,
      "distance": 4.849999904632568,
      "iconURL": "http://content.mqcdn.com/mqtraffic/congestion_mod.png",
      "parameterizedDescription": {
        "crossRoad2": "I-25  Exits 210A,210B / I-70 Bus Colfax Ave / Auraria Pkwy",
        "crossRoad1": "I-25  Exits 207B,208 / US-85 Santa Fe Dr / Kalamath St",
        "position2": "and",
        "direction": "Northbound",
        "position1": "between",
        "eventText": "XDFI Delays",
        "roadName": "I-25"
      }
    },
    {
      "id": "48356745",
      "type": 3,
      "severity": 2,
      "eventCode": 214,
      "lat": 39.751297,
      "lng": -105.016808,
      "startTime": "2017-06-30T11:24:00",
      "endTime": "2017-06-30T12:15:54",
      "impacting": true,
      "shortDesc": "I-25 S/B: delays of three minutes between Exit 213 I-70 and Exit 211 23rd Ave",
      "fullDesc": "Delays of three minutes on I-25 Southbound between Exit 213 I-70 and Exit 211 23rd Ave.",
      "delayFromFreeFlow": 2.680000066757202,
      "delayFromTypical": 1.9299999475479126,
      "distance": 3.509999990463257,
      "iconURL": "http://content.mqcdn.com/mqtraffic/congestion_mod.png",
      "parameterizedDescription": {
        "crossRoad2": "I-25  Exit 211 / 23rd Ave / Water St",
        "crossRoad1": "I-70  Exit 274 / I-25  Exit 213 / US-6",
        "position2": "and",
        "direction": "Southbound",
        "position1": "between",
        "eventText": "XDFI Delays",
        "roadName": "I-25"
      }
    },
    {
      "id": "48347260",
      "type": 3,
      "severity": 2,
      "eventCode": 74,
      "lat": 39.780136,
      "lng": -104.966866,
      "startTime": "2017-06-30T09:34:00",
      "endTime": "2017-06-30T12:20:39",
      "impacting": true,
      "shortDesc": "I-70 W/B: delays increasing between Exit 278 and Exit 275B",
      "fullDesc": "Delays increasing and delays of three minutes on I-70 Westbound between Exit 278 CO-35 Quebec St and Exit 275B CO-265 Brighton Blvd. Average speed 25 mph.",
      "delayFromFreeFlow": 3.9700000286102295,
      "delayFromTypical": 0,
      "distance": 5.429999828338623,
      "iconURL": "http://content.mqcdn.com/mqtraffic/congestion_mod.png",
      "parameterizedDescription": {
        "crossRoad2": "I-70  Exit 275B / CO-265 Brighton Blvd / Brighton Blvd",
        "crossRoad1": "I-70  Exit 278 / CO-35 Quebec St / CO-35 Northfield Quebec St",
        "position2": "and",
        "direction": "Westbound",
        "position1": "between",
        "eventText": "XDFI Delays increasing, xDFI Delays, xDFI Average Speed",
        "roadName": "I-70"
      }
    },
    {
      "id": "48347262",
      "type": 3,
      "severity": 1,
      "eventCode": 73,
      "lat": 39.804871,
      "lng": -104.939171,
      "startTime": "2017-06-30T09:28:00",
      "endTime": "2017-06-30T12:18:46",
      "impacting": true,
      "shortDesc": "I-270 W/B: delays of eight minutes in Commerce City",
      "fullDesc": "Delays of eight minutes and delays easing on I-270 Westbound in Commerce City. Average speed 15 mph.",
      "delayFromFreeFlow": 8.350000381469727,
      "delayFromTypical": 6.380000114440918,
      "distance": 5,
      "iconURL": "http://content.mqcdn.com/mqtraffic/congestion_min.png",
      "parameterizedDescription": {
        "crossRoad2": "I-270",
        "crossRoad1": "I-70  Exits 279A,279B / I-270  Exit 5 / Central Park Blvd",
        "position2": "and",
        "direction": "Westbound",
        "position1": "between",
        "eventText": "XDFI Delays, xDFI Delays easing, xDFI Average Speed",
        "roadName": "I-270"
      }
    }
  ],
  "mqUrl": "http://www.mapquest.com/maps?traffic=1&latitude=39.73831084129981&longitude=-104.9850082397461",
  "info": {
    "copyright": {
      "text": "© 2017 MapQuest, Inc.",
      "imageUrl": "https://api-s.mqcdn.com/res/mqlogo.gif",
      "imageAltText": "© 2017 MapQuest, Inc."
    },
    "messages": [],
    "statuscode": 0
  }
}

嗨,在上面的代码中,我想在上面的xml树中搜索一个特定的节点,比如说我对节点roadName:I-25感兴趣,并显示道路名称,那么有人能指导我怎么做吗?在元素树的帮助下在xml树中找到一个感兴趣的节点/子节点


Tags: andofcomhttpexitbetweenstco
1条回答
网友
1楼 · 发布于 2024-09-27 00:17:24

如注释中所述,您拥有的是JSON数据(请参阅相关:Python - Parsing JSON Data Set

使用json模块:

import json

# if data is a json string
json_data = json.loads(data) # or use json.load(data) if data is a file

要获得所需的值,请从数据中索引相应的键。在本例中,父节点是'incidents',您需要第二个列表项(零索引,因此使用[1]),然后向下搜索相应的键:

print json_data['incidents'][1]['parameterizedDescription']['roadName']

将输出:

I-25

相关问题 更多 >

    热门问题