import json import re import datetime filename = "location_history.json" with open(filename) as f: mydata = json.load(f) # print(mydata.keys()) #toplevelkeys = ['Frequent Locations', 'Latest Location', 'Home & Work', 'Daily Top Locations', 'Top Locations Per Six-Day Period', 'Location History', 'Businesses and public places you may have visited', 'Areas you may have visited in the last two years'] # Kopfzeile der CSV-Datei csv = "" # Muster zum extrahieren der Koordinaten geomuster = re.compile("([0-9.]+).*?, ([0-9.]+)") # Muster der Zeitangabe zeitformat = "%Y-%m-%d %H:%M:%S UTC" minlat = 100 maxlat = -100 minlon = 200 maxlon = -200 # Alle Anagaben in 'Location History' durchgehen und als CSV-Zeile anfügen for location in mydata['Location History']: # Extrahieren der geographischen Koordinaten # print(location) res = geomuster.search(location[1]) try: latitude = float(res.group(1)) longitude = float(res.group(2)) minlat = min([minlat, latitude]) maxlat = max([maxlat, latitude]) minlon = min([minlon, longitude]) maxlon = max([maxlon, longitude]) # Umwandlunmaxer Datum/Zeitangabe in internes Format dt = datetime.datetime.strptime(location[0], zeitformat) # Zeile in der CSV Datei csv += f"{dt},{latitude},{longitude}\n" except AttributeError: pass # Noch einen Header mit den minimalen/maximalen Koordinaten ausgeben csv = f"Time,Latitude,Longitude, \"minLong minLat maxLong maxLat\",{minlat},{minlon},{maxlat},{maxlon}\n"+csv # CSV Datei schreiben with open("location_history.csv","w") as f: f.write(csv)