Last active
March 10, 2017 22:35
-
-
Save fedorn/d286c4e6211421ddb35c8c98bb0735fb to your computer and use it in GitHub Desktop.
Average JSON files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import json | |
| import numbers | |
| from collections import defaultdict | |
| json_paths = sys.argv[1:] | |
| all_json = {} | |
| for json_path in json_paths: | |
| with open(json_path) as json_file: | |
| all_json[json_path] = json.load(json_file) | |
| # print(all_json) | |
| result_json = defaultdict(float) | |
| for key in all_json[json_paths[0]]: | |
| if isinstance(all_json[json_paths[0]][key], numbers.Number): | |
| for json_path, json_values in all_json.items(): | |
| result_json[key] += json_values[key] | |
| for key in result_json: | |
| result_json[key] /= len(all_json) | |
| print(json.dumps(result_json, sort_keys=True, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment