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':''
)
The first empty dictionary {}
is there because {"pytest_version": "7.1.e", "$report_type": "SessionStart"}
doesn't contain key 'label'
or 'outcome'
.
One solution might be using all()
to keep only the dictionaries which contains all keys in the key_list
:
logs = [
{"pytest_version": "7.1.e", "$report_type": "SessionStart"},
{"label": "test1", "outcome": "passed", "duration": 0.0009},
{"label": "test2", "outcome": "passed", "duration": 0.00019},
]
key_list = ["label", "outcome"]
print(
[{k: d[k] for k in key_list} for d in logs if all(k in d for k in key_list)]
)
Prints:
[{'label': 'test1', 'outcome': 'passed'}, {'label': 'test2', 'outcome': 'passed'}]