简体   繁体   中英

Extracting values from nested dictionary from text file to JSON

The text file contains dictionary of dictionary. In that text file for exmaple "2018" acts as they further "8" is the month which is value for "2018" but key for next dictionary. I want to fetch the "total_queries_count","total_dislike","unique_users" values.

{"2018":
    {"8":{ "total_queries_count": 4,
        "queries_without_teachers": 3,
        "non_teacher_queries": 1,
        "total_dislike": 0,
        "unique_users": [", "landmark", "232843"],
        "user_dislike": 0
        },
    "9":{ "total_queries_count": 1021,
     "queries_without_teachers": 0,
        "non_teacher_queries": 1021,
        "total_dislike": 0,
        "unique_users": [", "1465146", "14657", "dfgf", "1123", "456", "1461546", "Ra", "siva", "234", "ramesh", "3456", "23", "43567", "sfdf", "sdsd", "ra", "sddff", "1234", "rames", "RAM", "444", "123", "333", "RAM", "789", "itassistant", "rame", "12345"],
        "user_dislike": 0},
    "10": {"total_queries_count": 352,
     "queries_without_teachers": 1,
         "non_teacher_queries": 351,
         "total_dislike": 0,
         "unique_users": [", "1465146", "777", "43567", "1234", "456", "123456", "12345", "232843"],
         "user_dislike": 0
         },
    "11": {"total_queries_count": 180,
        "queries_without_teachers": 0,
            "non_teacher_queries": 180,
            "total_dislike": 12,
            "unique_users": [", "75757575", "9000115", "9000157", "9000494", "9000164", "123453"],
            "user_dislike": 12},
    "12": {"total_queries_count": 266,
        "queries_without_teachers": 0,
           "non_teacher_queries": 266,
           "total_dislike": 16,
           "unique_users": [", "131422", "121550", "9000508", "9000560", "9000115", "9000371", "9000372", "93979", "146625", "114586", "165937", "9000494", "9000463", "38404", "129458", "62948", "125143", "9000179", "9000145", "9000001", "9000164", "81849", "102663", "9000123", "105407", "33517", "21344", "9000213", "202074", "9000103", "18187", "9000342", "9000125", "9000100", "9000187", "18341", "9000181", "168802", "9000529", "12345", "110127", "9000134", "100190", "9000352", "9000156", "9000055", "tcs_hariharas", "9000078", "204101", "9000050", "9000139"],
           "user_dislike": 16}
          }
}

Check https://docs.python.org/3/tutorial/datastructures.html#dictionaries

You can access needed keys like this:

# assuming your initial nested dict is called 'data'
data["2018"]["8"]["total_queries_count"]

If you want to aggregate data for all years and months in one place, you can do this:

overall_queries = 0
overall_dislikes = 0
users = set()  # this is a set not a list in order to preserve uniqueness of users
for year in data:  # year is a key in data dict
    for month in data[year]:  # month is a key in data[year] dict
        users.update(data[year][month]["unique_users"])
        overall_queries += data[year][month]["total_queries_count"]
        overall_dislikes += data[year][month]["total_dislike"]

If you want to keep your result separated by years you can do this:

result = {}
for year in data:
    overall_queries = 0
    overall_dislikes = 0
    users = set()
    for month in data[year]:
        overall_queries += data[year][month]["total_queries_count"]
        overall_dislikes += data[year][month]["total_dislike"]
        users.update(data[year][month]["unique_users"])
    result[year] = {
        "overall_queries": overall_queries,
        "overall_dislikes": overall_dislikes,
        "users": users,
    }

Result:

{'2018': {'overall_dislikes': 28,
          'overall_queries': 1823,
          'users': {'100190',
                    '102663',
                    '105407',
                    '110127',
                    ...}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM