I am importing a log file with JSON-like dictionaries and attempting to: 1)only include logs where all three keys are included 2)create a new list of dictionaries that only have these key/values
log:
{"pytest_version": "7.1.e", "$report_type": "SessionStart"}
{"label": "test1", "outcome": "passed", "duration":0.0009}
{"label": "test2", "outcome": "passed", "duration":0.00019}
Script:
with open('log', 'r') as f:
key_list = ['label', 'outcome']
l = [json.loads(log) for log in logs]
l2 = [{k:v for k, v in d.items() if k in key_list} for d in l]
print(l2)
Output
[{},
{'label': 'test1', 'outcome': 'passed'},
{'label': 'test2', 'outcome': 'passed'}]
Why is there an empty dictionary in the results? And how could it be modified to only include results if the key has a value (e.g. exclude 'outcome':'')