简体   繁体   中英

How can I iterate through this list in Python

I'm trying through iterate through the list below and am having a hard time. I am trying to access the dictionary element "metrics". I am trying to print out all the key/values of 'timestamp' and 'values'.

I've been able to access the list using the code below and do see how I can individually get within the list but am not sure how to build a loop to get all values I need.

flat_file = DATA[0]['environments'][0]['dimensions'][0]

[
    {
        "environments": [
            {
                "dimensions": [
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "651.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "510.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "TestL"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "477.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "338.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "test_y"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "91.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "93.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "Testz"
                    }
                ],
                "name": "prod"
            }
        ],
        "metaData": {
            "errors": [],
            "notices": [
                "query served by: something",
                "Source:db",
                "Table used: something",
                "Metric with Avg of request_size was requested. For this a global avg was also computed with name global-avg-request_size"
            ]
        }
    }
]

This will work:

for i in range(len(l[0]['environments'][0]['dimensions'])):
    for j in range(len(l[0]['environments'][0]['dimensions'][i]['metrics'])):
        if '{' in str(l[0]['environments'][0]['dimensions'][i]['metrics'][j]['values']):
            print(l[0]['environments'][0]['dimensions'][i]['metrics'][j]['values'])

Output:

[{'timestamp': 1582588800000, 'value': '651.0'}, {'timestamp': 1582502400000, 'value': '510.0'}]
[{'timestamp': 1582588800000, 'value': '0.0'}, {'timestamp': 1582502400000, 'value': '0.0'}]
[{'timestamp': 1582588800000, 'value': '477.0'}, {'timestamp': 1582502400000, 'value': '338.0'}]
[{'timestamp': 1582588800000, 'value': '0.0'}, {'timestamp': 1582502400000, 'value': '0.0'}]
[{'timestamp': 1582588800000, 'value': '91.0'}, {'timestamp': 1582502400000, 'value': '93.0'}]
[{'timestamp': 1582588800000, 'value': '0.0'}, {'timestamp': 1582502400000, 'value': '0.0'}]

What you are reading is JSON, to read it in python you need :

import json

data = '''{
        "environments": [
            {
                "dimensions": [
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "651.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "510.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "TestL"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "477.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "338.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "test_y"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "91.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "93.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "Testz"
                    }
                ],
                "name": "prod"
            }
        ],
        "metaData": {
            "errors": [],
            "notices": [
                "query served by: something",
                "Source:db",
                "Table used: something",
                "Metric with Avg of request_size was requested. For this a global avg was also computed with name global-avg-request_size"
            ]
        }
    }'''

data_dict = json.loads(data)

This will create a python dictionary of your data. So now access the data as you will for a dictionary.

like: data_dict["environments"]

to go deeper try combo of for:

>>> for doc in data_dict['environments']:
...   for i in doc['dimensions']:
...     print(i['name'])
...
TestL
test_y
Testz

For better data oriented view and usage use Pandas check this link and this

lisp = [
{
    "environments": [
        {
            "dimensions": [
                {
                    "metrics": [
                        {
                            "name": "sum(message_count)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "651.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "510.0"
                                }
                            ]
                        },
                        {
                            "name": "global-avg-request_size",
                            "values": [
                                "0.0"
                            ]
                        },
                        {
                            "name": " avg(request_size)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "0.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "0.0"
                                }
                            ]
                        }
                    ],
                    "name": "TestL"
                },
                {
                    "metrics": [
                        {
                            "name": "sum(message_count)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "477.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "338.0"
                                }
                            ]
                        },
                        {
                            "name": "global-avg-request_size",
                            "values": [
                                "0.0"
                            ]
                        },
                        {
                            "name": " avg(request_size)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "0.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "0.0"
                                }
                            ]
                        }
                    ],
                    "name": "test_y"
                },
                {
                    "metrics": [
                        {
                            "name": "sum(message_count)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "91.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "93.0"
                                }
                            ]
                        },
                        {
                            "name": "global-avg-request_size",
                            "values": [
                                "0.0"
                            ]
                        },
                        {
                            "name": " avg(request_size)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "0.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "0.0"
                                }
                            ]
                        }
                    ],
                    "name": "Testz"
                }
            ],
            "name": "prod"
        }
    ],
    "metaData": {
        "errors": [],
        "notices": [
            "query served by: something",
            "Source:db",
            "Table used: something",
            "Metric with Avg of request_size was requested. For this a global avg was also computed with name global-avg-request_size"
        ]
    }
}
]

for element in lisp[0]['environments'][0]['dimensions']:

    for inner_element in element['metrics']:

        for el in inner_element['values']:

            if type(el) == dict:
                print(el)

{'timestamp': 1582588800000, 'value': '651.0'}
{'timestamp': 1582502400000, 'value': '510.0'}
{'timestamp': 1582588800000, 'value': '0.0'}
{'timestamp': 1582502400000, 'value': '0.0'}
{'timestamp': 1582588800000, 'value': '477.0'}
{'timestamp': 1582502400000, 'value': '338.0'}
{'timestamp': 1582588800000, 'value': '0.0'}
{'timestamp': 1582502400000, 'value': '0.0'}
{'timestamp': 1582588800000, 'value': '91.0'}
{'timestamp': 1582502400000, 'value': '93.0'}
{'timestamp': 1582588800000, 'value': '0.0'}
{'timestamp': 1582502400000, 'value': '0.0'}

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