0

Нужна ваша помощь. Имеется словарь:

a = 
      {
       "meta": {
          "request": {
             "main_domain_only": false,
             "format": "json",
             "domain": "stroсd.com",
             "start_date": "2019-10-01",
             "end_date": "2020-01-31",
             "limit": null,
             "country": "world",
             "show_verified": true
          },
          "status": "Success",
          "last_updated": "2020-01-31"
       },
       "overview": [
          {
             "domain": "Direct",
             "source_type": "Direct",
             "share": 0.55773467
          },
          {
             "domain": "Google Search",
             "source_type": "Search / Organic",
             "share": 0.2018796
          },
          {
             "domain": "Facebook",
             "source_type": "Social",
             "share": 0.081907906
          },
          {
             "domain": "Google Search",
             "source_type": "Search / Paid",
             "share": 0.035226278
          },
          {
             "domain": "Email",
             "source_type": "Email",
             "share": 0.026017865
          },
          {
             "domain": "Other Sources",
             "source_type": "Other",
             "share": 0.0070781517
          }
       ],
       "visits": 36721
    }

Как вывести первые 3 домена? То есть Direct, Google Search, Facebook.

print(a["overview"]["domain"][0][0][0:3])

Ошибка: TypeError: list indices must be integers or slices, not str

0

1 ответ 1

0

Изначально a у вас это JSON. JSON != Питонскому словарю.
Чтобы распарсить его, нужно импорировать пакет json.

a = """ 
    {
   "meta":{
      "request":{
         "main_domain_only":false,
         "format":"json",
         "domain":"stroсd.com",
         "start_date":"2019-10-01",
         "end_date":"2020-01-31",
         "limit":null,
         "country":"world",
         "show_verified":true
      },
      "status":"Success",
      "last_updated":"2020-01-31"
   },
   "overview":[
      {
         "domain":"Direct",
         "source_type":"Direct",
         "share":0.55773467
      },
      {
         "domain":"Google Search",
         "source_type":"Search / Organic",
         "share":0.2018796
      },
      {
         "domain":"Facebook",
         "source_type":"Social",
         "share":0.081907906
      },
      {
         "domain":"Google Search",
         "source_type":"Search / Paid",
         "share":0.035226278
      },
      {
         "domain":"Email",
         "source_type":"Email",
         "share":0.026017865
      },
      {
         "domain":"Other Sources",
         "source_type":"Other",
         "share":0.0070781517
      }
   ],
   "visits":36721
}
"""

import json
domains = [i['domain'] for i in json.loads(a)['overview']][:3]
2
  • Спасибо! А без импорта json никак? По индексам просто нельзя?
    – Ray
    26 фев 2020 в 11:51
  • @Mer, если мы просто создадим словарь, так как у вас с json'a, то будет ошибка. В Питоне нету false\true, есть False\True, типа null и т.д.
    – entithat
    26 фев 2020 в 17:12

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.