#!/usr/bin/python # -*- coding: utf-8 -*- from json_extensions import load_from_json_file, save_to_json_file def main(): clean_data: dict[str, list] = {} obj = load_from_json_file('all_measurements.json') names = set() for row in obj: name = row.get('name', '').strip('\n') perc_s = row.get('perc', '').strip('\n') charging = row.get('charging') epoch = row.get('epoch') batt_full_s = row.get('batt_full', '').strip('\n') batt_full = 'The battery is full' in batt_full_s if '%' not in perc_s: raise ValueError(f'Missing % in {row}') prc = int(perc_s.split('%', maxsplit=1)[0]) names.add(name) if name not in clean_data: clean_data[name] = [] new_row = {'epoch': epoch, 'soc': prc, 'battery_full': batt_full, 'charging': charging} print(name, new_row) clean_data[name].append(new_row) print(names) for lst in clean_data.values(): lst.sort(key=lambda x: x['epoch']) save_to_json_file('clean_data.json', clean_data) if __name__ == '__main__': main()