fbpx

Проблема с парсингом Json

499 просмотраjson python-3.6
0
0 Комментариев
data = {
  "state": 1,
  "error": {
    "code": 0
  },
  "queries": [
    {
      "uuid": "bffbb822-867c70ea-3c1dd88b-d85be972",
      "command": {
        "plm_type_code": 506,
        "state": 2,
        "left_dims": [
          "b0b1529c",
          "e815d1e8"
        ],
        "top_dims": [],
        "top_flags": [
          0
        ],
        "left_flags": [
          1,
          0
        ],
        "total_row": 65,
        "total_col": 1,
        "from_row": 0,
        "from_col": 0,
        "top": [
          [
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 4,
              "fact_id": "3dd4d6ff",
              "flags": 0
            }
          ]
        ],
        "left": [
          [
            {
              "type": 2,
              "value": "DEO",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "DRS",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "BIL",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "HAR",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "HHC",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "ICR",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "ORL",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "OTH",
              "flags": 3
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "SAV",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "SCL",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "SCR",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "MTR",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ATK",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "SPR",
              "flags": 3
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "TEA",
              "flags": 3
            },
            {
              "type": 2,
              "value": "X5",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "OTH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 2,
              "value": "ACH",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 2,
              "value": "(Пустой)",
              "flags": 2
            },
            {
              "type": 2,
              "value": "(Пустой)",
              "flags": 0
            }
          ],
          [
            {
              "type": 1
            },
            {
              "type": 5,
              "flags": 0
            }
          ],
          [
            {
              "type": 5,
              "flags": 0
            },
            {
              "type": 1
            }
          ]
        ],
        "data": [
          [
            2993
          ],
          [
            21259
          ],
          [
            6618
          ],
          [
            2465
          ],
          [
            313
          ],
          [
            33648
          ],
          [
            908
          ],
          [
            3702
          ],
          [
            110
          ],
          [
            1284
          ],
          [
            30
          ],
          [
            6034
          ],
          [
            1318
          ],
          [
            7634
          ],
          [
            9009
          ],
          [
            1057
          ],
          [
            674
          ],
          [
            19692
          ],
          [
            255
          ],
          [
            1847
          ],
          [
            13994
          ],
          [
            224
          ],
          [
            414
          ],
          [
            16734
          ],
          [
            2645
          ],
          [
            9940
          ],
          [
            233
          ],
          [
            111
          ],
          [
            12929
          ],
          [
            1639
          ],
          [
            2228
          ],
          [
            1548
          ],
          [
            114
          ],
          [
            158
          ],
          [
            5687
          ],
          [
            5
          ],
          [
            2
          ],
          [
            7
          ],
          [
            847
          ],
          [
            2576
          ],
          [
            573
          ],
          [
            36
          ],
          [
            4032
          ],
          [
            1115
          ],
          [
            5912
          ],
          [
            2818
          ],
          [
            139
          ],
          [
            31
          ],
          [
            10015
          ],
          [
            2953
          ],
          [
            14558
          ],
          [
            3103
          ],
          [
            719
          ],
          [
            362
          ],
          [
            21695
          ],
          [
            101
          ],
          [
            2
          ],
          [
            103
          ],
          [
            1421
          ],
          [
            6823
          ],
          [
            111
          ],
          [
            8355
          ],
          [
            0
          ],
          [
            0
          ],
          [
            138931
          ]
        ]
      }
    }
  ]
}
   top = 	data['queries'][0]['command']['top']
left = data['queries'][0]['command']['left']
numbers = data['queries'][0]['command']['data']

Коллеги,приветствую!
Нужно достать названия,которые содержатся в «value» и добавить их в список
Проблема в том,что внутри этого массива данных,есть пустые словари,которые не содержат «value» и,собственно,питон выдает ошибку KeyError
Понимаю,что нужно это обернуть в try — except, но вот написать это сам не могу
Буду признателен вам за помощь 🙂


Добавить комментарий

1 Ответы

Python Опубликовано 20.05.2019
0
Сергей (анонимный) 0 Comments

не нужно try — except
у словаря есть метод get — вторым параметром передаётся дефолтное значение
d.get(key, default)
или https://docs.python.org/3/library/stdtypes.html#dict.setdefault

Добавить комментарий
Напишите свой ответ на данный вопрос.
Scroll Up