首页 > 解决方案 > 按某些字符串值拆分 Json 数据(Python)

问题描述

我想按"incidentType"python 的值拆分事件。它始终具有以下 5 个值:期间、伤病时间、进球、卡片和替换。

json文件

{
  "incidents": [
    {
      "text": "FT",
      "homeScore": 2,
      "awayScore": 1,
      "isLive": false,
      "time": 90,
      "addedTime": 999,
      "incidentType": "period"
    },
    {
      "length": 4,
      "time": 90,
      "addedTime": 0,
      "incidentType": "injuryTime"
    },
    {
      "homeScore": 2,
      "awayScore": 1,
      "player": {
        "name": "Mostafa Mohamed",
        "firstName": "",
        "lastName": "",
        "slug": "mostafa-mohamed",
        "shortName": "M. Mohamed",
        "position": "F",
        "userCount": 3949,
        "id": 873551
      },
      "id": 141786584,
      "time": 89,
      "isHome": true,
      "incidentClass": "penalty",
      "incidentType": "goal"
    },
    {
      "player": {
        "name": "Duško Tošić",
        "slug": "dusko-tosic",
        "shortName": "D. Tošić",
        "position": "D",
        "userCount": 215,
        "id": 14557
      },
      "playerName": "Duško Tošić",
      "reason": "Foul",
      "id": 119728583,
      "time": 85,
      "isHome": false,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "playerIn": {
        "name": "Younès Belhanda",
        "slug": "younes-belhanda",
        "shortName": "Y. Belhanda",
        "position": "M",
        "userCount": 2165,
        "id": 72999
      },
      "playerOut": {
        "name": "Martin Linnes",
        "slug": "martin-linnes",
        "shortName": "M. Linnes",
        "position": "D",
        "userCount": 339,
        "id": 109569
      },
      "id": 120059400,
      "time": 82,
      "isHome": true,
      "incidentType": "substitution"
    },
    {
      "player": {
        "name": "Kevin Varga",
        "slug": "kevin-varga",
        "shortName": "K. Varga",
        "position": "M",
        "userCount": 274,
        "id": 602730
      },
      "playerName": "Kevin Varga",
      "reason": "Foul",
      "id": 119728582,
      "time": 82,
      "isHome": false,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "playerIn": {
        "name": "DeAndre Yedlin",
        "slug": "deandre-yedlin",
        "shortName": "D. Yedlin",
        "position": "D",
        "userCount": 702,
        "id": 314040
      },
      "playerOut": {
        "name": "Muhammed Kerem Aktürkoğlu",
        "firstName": "",
        "lastName": "",
        "slug": "muhammed-kerem-akturkoglu",
        "shortName": "M. K. Aktürkoğlu",
        "position": "F",
        "userCount": 281,
        "id": 903324
      },
      "id": 120059399,
      "time": 77,
      "isHome": true,
      "incidentType": "substitution"
    },
    {
      "playerIn": {
        "name": "Ryan Donk",
        "slug": "ryan-donk",
        "shortName": "R. Donk",
        "position": "D",
        "userCount": 489,
        "id": 14900
      },
      "playerOut": {
        "name": "Ryan Babel",
        "slug": "ryan-babel",
        "shortName": "R. Babel",
        "position": "F",
        "userCount": 1577,
        "id": 1876
      },
      "id": 120059397,
      "time": 72,
      "isHome": true,
      "incidentType": "substitution"
    },
    {
      "playerIn": {
        "name": "Emre Akbaba",
        "slug": "emre-akbaba",
        "shortName": "E. Akbaba",
        "position": "M",
        "userCount": 604,
        "id": 343527
      },
      "playerOut": {
        "name": "Gedson Fernandes",
        "slug": "fernandes-gedson",
        "shortName": "G. Fernandes",
        "position": "M",
        "userCount": 3030,
        "id": 862055
      },
      "id": 120059396,
      "time": 71,
      "isHome": true,
      "incidentType": "substitution"
    },
    {
      "playerIn": {
        "name": "Henry Onyekuru",
        "slug": "henry-onyekuru",
        "shortName": "H. Onyekuru",
        "position": "M",
        "userCount": 1474,
        "id": 809220
      },
      "playerOut": {
        "name": "Emre Kılınç",
        "slug": "emre-kilinc",
        "shortName": "E. Kılınç",
        "position": "M",
        "userCount": 526,
        "id": 202032
      },
      "id": 120059398,
      "time": 71,
      "isHome": true,
      "incidentType": "substitution"
    },
    {
      "player": {
        "name": "Haris Hajradinović",
        "slug": "haris-hajradinovic",
        "shortName": "H. Hajradinović",
        "position": "M",
        "userCount": 357,
        "id": 254979
      },
      "playerName": "Haris Hajradinović",
      "reason": "Foul",
      "id": 119728581,
      "time": 71,
      "isHome": false,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "homeScore": 1,
      "awayScore": 1,
      "player": {
        "name": "Isaac Kiese Thelin",
        "slug": "isaac-kiese-thelin",
        "shortName": "I. K. Thelin",
        "position": "F",
        "userCount": 386,
        "id": 178743
      },
      "assist1": {
        "name": "Haris Hajradinović",
        "slug": "haris-hajradinovic",
        "shortName": "H. Hajradinović",
        "position": "M",
        "userCount": 357,
        "id": 254979
      },
      "id": 141786585,
      "time": 51,
      "isHome": false,
      "incidentClass": "regular",
      "incidentType": "goal"
    },
    {
      "playerIn": {
        "name": "Kevin Varga",
        "slug": "kevin-varga",
        "shortName": "K. Varga",
        "position": "M",
        "userCount": 274,
        "id": 602730
      },
      "playerOut": {
        "name": "Gilbert Koomson",
        "slug": "gilbert-koomson",
        "shortName": "G. Koomson",
        "position": "F",
        "userCount": 76,
        "id": 341107
      },
      "id": 120059401,
      "time": 46,
      "isHome": false,
      "incidentType": "substitution"
    },
    {
      "text": "HT",
      "homeScore": 1,
      "awayScore": 0,
      "isLive": false,
      "time": 45,
      "addedTime": 999,
      "incidentType": "period"
    },
    {
      "player": {
        "name": "Isaac Kiese Thelin",
        "slug": "isaac-kiese-thelin",
        "shortName": "I. K. Thelin",
        "position": "F",
        "userCount": 386,
        "id": 178743
      },
      "playerName": "Isaac Kiese Thelin",
      "reason": "Foul",
      "id": 119728580,
      "time": 15,
      "isHome": false,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "homeScore": 1,
      "awayScore": 0,
      "player": {
        "name": "Muhammed Kerem Aktürkoğlu",
        "firstName": "",
        "lastName": "",
        "slug": "muhammed-kerem-akturkoglu",
        "shortName": "M. K. Aktürkoğlu",
        "position": "F",
        "userCount": 281,
        "id": 903324
      },
      "id": 141786583,
      "time": 9,
      "isHome": true,
      "incidentClass": "regular",
      "incidentType": "goal"
    }
  ]
}

标签: pythonjson

解决方案


ABC = {
  "incidents": [
    {
      "text": "FT",
      "homeScore": 2,
      "awayScore": 1,
      "isLive": False,
      "time": 90,
      "addedTime": 999,
      "incidentType": "period"
    },
    {
      "length": 4,
      "time": 90,
      "addedTime": 0,
      "incidentType": "injuryTime"
    },
    {
      "homeScore": 2,
      "awayScore": 1,
      "player": {
        "name": "Mostafa Mohamed",
        "firstName": "",
        "lastName": "",
        "slug": "mostafa-mohamed",
        "shortName": "M. Mohamed",
        "position": "F",
        "userCount": 3949,
        "id": 873551
      },
      "id": 141786584,
      "time": 89,
      "isHome": True,
      "incidentClass": "penalty",
      "incidentType": "goal"
    },
    {
      "player": {
        "name": "Duško Tošić",
        "slug": "dusko-tosic",
        "shortName": "D. Tošić",
        "position": "D",
        "userCount": 215,
        "id": 14557
      },
      "playerName": "Duško Tošić",
      "reason": "Foul",
      "id": 119728583,
      "time": 85,
      "isHome": False,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "playerIn": {
        "name": "Younès Belhanda",
        "slug": "younes-belhanda",
        "shortName": "Y. Belhanda",
        "position": "M",
        "userCount": 2165,
        "id": 72999
      },
      "playerOut": {
        "name": "Martin Linnes",
        "slug": "martin-linnes",
        "shortName": "M. Linnes",
        "position": "D",
        "userCount": 339,
        "id": 109569
      },
      "id": 120059400,
      "time": 82,
      "isHome": True,
      "incidentType": "substitution"
    },
    {
      "player": {
        "name": "Kevin Varga",
        "slug": "kevin-varga",
        "shortName": "K. Varga",
        "position": "M",
        "userCount": 274,
        "id": 602730
      },
      "playerName": "Kevin Varga",
      "reason": "Foul",
      "id": 119728582,
      "time": 82,
      "isHome": False,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "playerIn": {
        "name": "DeAndre Yedlin",
        "slug": "deandre-yedlin",
        "shortName": "D. Yedlin",
        "position": "D",
        "userCount": 702,
        "id": 314040
      },
      "playerOut": {
        "name": "Muhammed Kerem Aktürkoğlu",
        "firstName": "",
        "lastName": "",
        "slug": "muhammed-kerem-akturkoglu",
        "shortName": "M. K. Aktürkoğlu",
        "position": "F",
        "userCount": 281,
        "id": 903324
      },
      "id": 120059399,
      "time": 77,
      "isHome": True,
      "incidentType": "substitution"
    },
    {
      "playerIn": {
        "name": "Ryan Donk",
        "slug": "ryan-donk",
        "shortName": "R. Donk",
        "position": "D",
        "userCount": 489,
        "id": 14900
      },
      "playerOut": {
        "name": "Ryan Babel",
        "slug": "ryan-babel",
        "shortName": "R. Babel",
        "position": "F",
        "userCount": 1577,
        "id": 1876
      },
      "id": 120059397,
      "time": 72,
      "isHome": True,
      "incidentType": "substitution"
    },
    {
      "playerIn": {
        "name": "Emre Akbaba",
        "slug": "emre-akbaba",
        "shortName": "E. Akbaba",
        "position": "M",
        "userCount": 604,
        "id": 343527
      },
      "playerOut": {
        "name": "Gedson Fernandes",
        "slug": "fernandes-gedson",
        "shortName": "G. Fernandes",
        "position": "M",
        "userCount": 3030,
        "id": 862055
      },
      "id": 120059396,
      "time": 71,
      "isHome": True,
      "incidentType": "substitution"
    },
    {
      "playerIn": {
        "name": "Henry Onyekuru",
        "slug": "henry-onyekuru",
        "shortName": "H. Onyekuru",
        "position": "M",
        "userCount": 1474,
        "id": 809220
      },
      "playerOut": {
        "name": "Emre Kılınç",
        "slug": "emre-kilinc",
        "shortName": "E. Kılınç",
        "position": "M",
        "userCount": 526,
        "id": 202032
      },
      "id": 120059398,
      "time": 71,
      "isHome": True,
      "incidentType": "substitution"
    },
    {
      "player": {
        "name": "Haris Hajradinović",
        "slug": "haris-hajradinovic",
        "shortName": "H. Hajradinović",
        "position": "M",
        "userCount": 357,
        "id": 254979
      },
      "playerName": "Haris Hajradinović",
      "reason": "Foul",
      "id": 119728581,
      "time": 71,
      "isHome": False,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "homeScore": 1,
      "awayScore": 1,
      "player": {
        "name": "Isaac Kiese Thelin",
        "slug": "isaac-kiese-thelin",
        "shortName": "I. K. Thelin",
        "position": "F",
        "userCount": 386,
        "id": 178743
      },
      "assist1": {
        "name": "Haris Hajradinović",
        "slug": "haris-hajradinovic",
        "shortName": "H. Hajradinović",
        "position": "M",
        "userCount": 357,
        "id": 254979
      },
      "id": 141786585,
      "time": 51,
      "isHome": False,
      "incidentClass": "regular",
      "incidentType": "goal"
    },
    {
      "playerIn": {
        "name": "Kevin Varga",
        "slug": "kevin-varga",
        "shortName": "K. Varga",
        "position": "M",
        "userCount": 274,
        "id": 602730
      },
      "playerOut": {
        "name": "Gilbert Koomson",
        "slug": "gilbert-koomson",
        "shortName": "G. Koomson",
        "position": "F",
        "userCount": 76,
        "id": 341107
      },
      "id": 120059401,
      "time": 46,
      "isHome": False,
      "incidentType": "substitution"
    },
    {
      "text": "HT",
      "homeScore": 1,
      "awayScore": 0,
      "isLive": False,
      "time": 45,
      "addedTime": 999,
      "incidentType": "period"
    },
    {
      "player": {
        "name": "Isaac Kiese Thelin",
        "slug": "isaac-kiese-thelin",
        "shortName": "I. K. Thelin",
        "position": "F",
        "userCount": 386,
        "id": 178743
      },
      "playerName": "Isaac Kiese Thelin",
      "reason": "Foul",
      "id": 119728580,
      "time": 15,
      "isHome": False,
      "incidentClass": "yellow",
      "incidentType": "card"
    },
    {
      "homeScore": 1,
      "awayScore": 0,
      "player": {
        "name": "Muhammed Kerem Aktürkoğlu",
        "firstName": "",
        "lastName": "",
        "slug": "muhammed-kerem-akturkoglu",
        "shortName": "M. K. Aktürkoğlu",
        "position": "F",
        "userCount": 281,
        "id": 903324
      },
      "id": 141786583,
      "time": 9,
      "isHome": True,
      "incidentClass": "regular",
      "incidentType": "goal"
    }
  ]
}

首先,创建一个字典来保存所有 distinct incidentType。然后遍历incidents并检查字典中是否incidentType存在。如果存在?附加。如果没有,创建一个新的key : value

result = {}
for js in ABC["incidents"]:
    icdType = js["incidentType"]
    if icdType in result:
        result[icdType].append(js)
    else:
        result[icdType] = [js]

for key,val in result.items():
    print(key, ":", val, "\n")

推荐阅读