Как полностью сплющить JSON для извлечения в CSV - PullRequest
0 голосов
/ 01 декабря 2018

У меня есть файл json, который я частично сгладил, используя примеры, которые я нашел через stackoverflow.Тем не менее, у меня все еще нет полностью сплющенного JSON, поэтому я не могу извлечь его в CSV.Я вставляю свой полный код вместе с ошибками здесь.Любая помощь будет принята с благодарностью!

postcodes = ['P5N2J1', 'P5N2J2','P5N2J3']
response = []

for postcode in postcodes:
    rr = requests.get('https://represent.opennorth.ca/postcodes/{}'.format(postcode))
    data = json.loads(rr.text)
    response.append(data)

#print(response)

x = response 

def flatten(items):
    non_list_items = []

    for item in items:
        if isinstance(item, list):
            for inner_item in flatten(item):
                yield inner_item
        else:
            non_list_items.append(item)

    yield non_list_items

flatten(x)

x

ВЫХОД:

{'representatives_centroid': [{'url': 'https://www.ola.org/en/members/all/guy-bourgouin',
   'email': 'GBourgouin-QP@ndp.on.ca',
   'personal_url': '',
   'related': {'boundary_url': '/boundaries/ontario-electoral-districts-representation-act-2015/mushkegowuk-james-bay/',
    'representative_set_url': '/representative-sets/ontario-legislature/'},
   'offices': [{'tel': '1 416 326-7351', 'type': 'legislature'},
    {'tel': '1 705 335-6400', 'type': 'constituency'}],
   'photo_url': 'https://www.ola.org/sites/default/files/member/profile-photo/guy_bourgouin.jpg',
   'representative_set_name': 'Legislative Assembly of Ontario',
   'party_name': 'New Democratic Party of Ontario',
   'gender': '',
   'district_name': 'Mushkegowuk—James Bay',
   'source_url': 'https://www.ola.org/en/members/current/contact-information',
   'last_name': 'Bourgouin',
   'name': 'Guy Bourgouin',
   'first_name': 'Guy',
   'extra': {},
   'elected_office': 'MPP'},
  {'url': 'http://www.parl.gc.ca/Parliamentarians/en/members/Carol-Hughes(31289)',
   'email': 'carol.hughes@parl.gc.ca',
   'personal_url': 'http://carolhughes.ndp.ca',
   'related': {'boundary_url': '/boundaries/federal-electoral-districts/35002/',
    'representative_set_url': '/representative-sets/house-of-commons/'},
   'offices': [{'postal': 'House of Commons\nOttawa ON  K1A 0A6',
     'tel': '1 613 996-5376',
     'fax': '1 613 995-6661',
     'type': 'legislature'},
    {'postal': '289-A Hillside Drive South (Main Office)\n\nElliot Lake ON  P5A 1N7',
     'tel': '1 705 848-8080',
     'fax': '1 705 848-1818',
     'type': 'constituency'},
    {'postal': '12-B Byng Road\n\nKapuskasing ON  P5N 1W3',
     'tel': '1 705 335-5533',
     'fax': '1 705 337-6869',
     'type': 'constituency'}],
   'photo_url': 'http://www.ourcommons.ca/Parliamentarians/Images/OfficialMPPhotos/42/HughesCarol_NDP.jpg',
   'representative_set_name': 'House of Commons',
   'party_name': 'NDP',
   'gender': '',
   'district_name': 'Algoma—Manitoulin—Kapuskasing',
   'source_url': 'http://www.parl.gc.ca/Parliamentarians/en/members?view=ListAll',
   'last_name': 'Hughes',
   'name': 'Carol Hughes',
   'first_name': 'Carol',
   'extra': {},
   'elected_office': 'MP'},
  {'url': '',
   'email': 'general@kapuskasing.ca',
   'personal_url': '',
   'related': {'boundary_url': '/boundaries/census-subdivisions/3556066/',
    'representative_set_url': '/representative-sets/campaign-set-4/'},
   'offices': [{'tel': '1 705 335-2341'}],
   'photo_url': '',
   'representative_set_name': 'Municipal councils without subdivisions in Canada',
   'party_name': '',
   'gender': 'M',
   'district_name': 'Kapuskasing',
   'source_url': 'http://www.kapuskasing.ca/en/governing/Mayor-and-Council.aspx',
   'last_name': 'Spacek',
   'name': 'Alan Spacek',
   'first_name': 'Alan',
   'extra': {'address': '88 Riverside Drive, Kapuskasing,\nON, P5N 1B3',
    'organization': 'Kapuskasing, Town of',
    'validation': 'TRUE',
    'office_type': 'legislature'},
   'elected_office': 'Mayor'}],
 'boundaries_centroid': [{'url': '/boundaries/federal-electoral-districts/35002/',
   'name': 'Algoma—Manitoulin—Kapuskasing',
   'external_id': '35002',
   'boundary_set_name': 'Federal electoral district',
   'related': {'boundary_set_url': '/boundary-sets/federal-electoral-districts/'}},
  {'url': '/boundaries/ontario-electoral-districts-representation-act-2015/mushkegowuk-james-bay/',
   'name': 'Mushkegowuk—James Bay',
   'external_id': '124',
   'boundary_set_name': 'Ontario electoral district',
   'related': {'boundary_set_url': '/boundary-sets/ontario-electoral-districts-representation-act-2015/'}},
  {'url': '/boundaries/ontario-electoral-districts-representation-act-2005/timmins-james-bay/',
   'name': 'Timmins—James Bay',
   'external_id': '93',
   'boundary_set_name': 'Ontario electoral district',
   'related': {'boundary_set_url': '/boundary-sets/ontario-electoral-districts-representation-act-2005/'}},
  {'url': '/boundaries/federal-electoral-districts-2003-representation-order/35002/',
   'name': 'Algoma—Manitoulin—Kapuskasing',
   'external_id': '35002',
   'boundary_set_name': 'Federal electoral district',
   'related': {'boundary_set_url': '/boundary-sets/federal-electoral-districts-2003-representation-order/'}},
  {'url': '/boundaries/census-divisions/3556/',
   'name': 'Cochrane',
   'external_id': '3556',
   'boundary_set_name': 'Census division',
   'related': {'boundary_set_url': '/boundary-sets/census-divisions/'}},
  {'url': '/boundaries/census-subdivisions/3556066/',
   'name': 'Kapuskasing',
   'external_id': '3556066',
   'boundary_set_name': 'Census subdivision',
   'related': {'boundary_set_url': '/boundary-sets/census-subdivisions/'}}],
 'boundaries_concordance': [],
 'province': 'ON',
 'code': 'P5N2J1',
 'centroid': {'coordinates': [-82.40755, 49.42085], 'type': 'Point'},
 'city': 'Kapuskasing'}

Извлечение в CSV:

f = csv.writer(open("sample.csv","w+"))

f.writerow(["name", "elected_office", "district_name", "code"])
for x in x:
    f.writerow([x["representatives_centroid"]["name"],
                x["representatives_centroid"]["elected_office"],
                x["representatives_centroid"]["district_name"],
                x["code"]
               ])

СООБЩЕНИЕ ОБ ОШИБКЕ:


TypeError                                 Traceback (most recent call last)
<ipython-input-55-3da67b2acc45> in <module>()
      3 f.writerow(["name", "elected_office", "district_name", "code"])
      4 for x in x:
----> 5     f.writerow([x["representatives_centroid"]["name"],
      6                 x["representatives_centroid"]["elected_office"],
      7                 x["representatives_centroid"]["district_name"],

TypeError: string indices must be integers

1 Ответ

0 голосов
/ 01 декабря 2018

Поскольку x["representatives_centriod"] возвращает list, а не dict, вы можете сохранить данные, добавив вложенный цикл.Кроме того, "code" содержится в x, а не в x["representatives_centriod"].Таким образом, вы должны хранить данные "code" перед вложенным циклом.

Например,

for i in x:
    code = i['code']
    for j in i['representatives_centroid']:
         f.writerow([j['name'], j['elected_office'], j['district_name'], code])
...