Как извлечь объекты из вложенных списков из файла Json с Python? - PullRequest
1 голос
/ 30 января 2020

Я получил ответ от Lobbyview в виде json. Я пытался поместить его в фрейм данных, чтобы получить доступ только к некоторым переменным, но безуспешно. Как я могу получить доступ только к некоторым переменным, таким как идентификатор и комитеты в формате, экспортируемом в .dta? Вот код, который я попробовал.

import requests, json
query = {"naics": "424430"}
results = requests.post('https://www.lobbyview.org/public/api/reports',
data = json.dumps(query))
print(results.json())

import pandas as pd
b = pd.DataFrame(results.json())

_id = data["_id"]
committee = data["_source"]["specific_issues"][0]["bills_by_algo"][0]["committees"]

Наблюдение за json выглядит так:

"_score": 4.421936, 
"_type": "object", 
"_id": "5EZUMbQp3hGKH8Uq2Vxuke", 
"_source": 
    {
    "issue_codes": ["CPT"], 
    "received": 1214320148, 
    "client_name": "INTELLECTUAL PROPERTY OWNERS ASSOCIATION", 
    "amount": 240000, 
    "client": 
        {
        "legal_name": "INTELLECTUAL PROPERTY OWNERS ASSOCIATION", 
        "name": "INTELLECTUAL PROPERTY OWNERS ASSOCIATION", 
        "naics": null, 
        "gvkey": null, 
        "ticker": "Unlisted", 
        "id": null, 
        "bvdid": "US131283992L"}, 
    "specific_issues": [
        {
        "text": "H.R. 34, H.R. 1908, H.R. 2336, H.R. 3093  S. 522, S. 681, S. 1145, S. 1745", 
        "bills_by_algo": [
            {
            "titles": ["To amend title 35, United States Code, to provide for patent reform.", "Patent Reform Act of 2007", "Patent Reform Act of 2007", "Patent Reform Act of 2007"], 
            "top_terms": ["Commerce", "Administrative fees"], 
            "sponsor": 
                {
                "firstname": "Howard", 
                "district": 28, 
                "title": "rep", 
                "id": 400025
                }, 
            "committees": ["House Judiciary"], 
            "introduced": 1176868800, 
            "type": "HR", "id": "110_HR1908"}, 
            {
            "titles": ["To amend title 35, United States Code, relating to the funding of the United States Patent and Trademark Office."], 
            "top_terms": ["Commerce", "Administrative fees"], 
            "sponsor": 
                {
                "firstname": "Howard", 
                "district": 28, 
                "title": "rep", 
                "id": 400025
                }, 
            "committees": ["House Judiciary"], 
            "introduced": 1179288000, 
            "type": "HR", 
            "id": "110_HR2336"
        }],

        "gov_entities": ["U.S. House of Representatives", "Patent and Trademark Office (USPTO)", "U.S. Senate", "UNDETERMINED", "U.S. Trade Representative (USTR)"], 
        "lobbyists": ["Valente, Thomas Silvio", "Wamsley, Herbert C"], 
        "year": 2007, 
        "issue": "CPT", 
        "id": "S4nijtRn9Q5NACAmbqFjvZ"}], 
    "year": 2007, 
    "is_latest_amendment": true,
     "type": "MID-YEAR AMENDMENT", 
    "id": "1466CDCD-BA3D-41CE-B7A1-F9566573611A", 
    "alternate_name": "INTELLECTUAL PROPERTY OWNERS ASSOCIATION"
    }, 
"_index": "collapsed"}```

1 Ответ

1 голос
/ 30 января 2020

Поскольку указанные вами данные довольно глубоко вложены в JSON -ответ, вам нужно l oop просмотреть их и временно сохранить в списке. Чтобы лучше понять данные ответов, я бы посоветовал вам использовать какой-либо инструмент для просмотра структуры JSON, например online JSON -Viewer . Не каждая запись в JSON содержит необходимые данные, поэтому я пытаюсь поймать ошибку через try и except. Чтобы убедиться, что id и committees соответствуют друг другу, я решил добавить их в список как маленькие диктовки. Этот список можно легко прочитать в Pandas. При сохранении в .dta требуется преобразовать списки внутри столбца committees в строки, вместо этого можно также сохранить как .csv для более общего формата.

import requests, json
import pandas as pd


query = {"naics": "424430"}
results = requests.post(
    "https://www.lobbyview.org/public/api/reports", data=json.dumps(query)
)


json_response = results.json()["result"]

# to save the JSON response
# with open("data.json", "w") as outfile:
#     json.dump(results.json()["result"], outfile)

resulting_data = []

# loop through the response
for data in json_response:
    # try to find entries with specific issues, bills_by_algo and committees
    try:
        # loop through the special issues
        for special_issue in data["specific_issues"]:
            _id = special_issue["id"]
            # loop through the bills_by_algo's
            for x in special_issue["bills_by_algo"]:
                # append the id and committees in a dict
                resulting_data.append(({"id": _id, "committees": x["committees"]}))

    except KeyError as e:
        print(e, "not found in entry.")
        continue


# create a DataFrame
df = pd.DataFrame(resulting_data)
# export of list objects in the column is not supported by .dta, therefore we convert
# to strings with ";" as delimiter
df["committees"] = ["; ".join(map(str, l)) for l in df["committees"]]
print(df)
df.to_stata("result.dta")


Результаты в

                         id                                         committees
0    D8BxG5664FFb8AVc6KTphJ                                    House Judiciary
1    D8BxG5664FFb8AVc6KTphJ                                   Senate Judiciary
2    8XQE5wu3mU7qvVPDpUWaGP                                  House Agriculture
3    8XQE5wu3mU7qvVPDpUWaGP        Senate Agriculture, Nutrition, and Forestry
4    kzZRLAHdMK4YCUQtQAdCPY                                  House Agriculture
..                      ...                                                ...
406  ZxXooeLGVAKec9W2i32hL5                                  House Agriculture
407  ZxXooeLGVAKec9W2i32hL5  Senate Agriculture, Nutrition, and Forestry; H...
408  ZxXooeLGVAKec9W2i32hL5        House Appropriations; Senate Appropriations
409  ahmmafKLfRP8wZay9o8GRf                                  House Agriculture
410  ahmmafKLfRP8wZay9o8GRf        Senate Agriculture, Nutrition, and Forestry

[411 rows x 2 columns]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...