Skip to content

Instantly share code, notes, and snippets.

@emilhorlyck
Last active September 7, 2020 09:28
Show Gist options
  • Save emilhorlyck/dea7a4d494f65e13a95d156dc049cc31 to your computer and use it in GitHub Desktop.
Save emilhorlyck/dea7a4d494f65e13a95d156dc049cc31 to your computer and use it in GitHub Desktop.
Python script to extract blood gluscose values to CSV from an Apple healthkit data export.
## Place the script in th folder with the healthkit export and make sure the export is named eksport.xml
# Rum the script in place and wait for the csv to be generated.
from xml.etree import ElementTree
import os
import csv
tree = ElementTree.parse("eksport.xml")
# Glucose measurements
csv_data = open("eksport_bg.csv", "w", newline="", encoding="utf-8")
csvWirter = csv.writer(csv_data)
root = tree.getroot()
for record in root.iter("Record"):
if record.attrib["type"] == "HKQuantityTypeIdentifierBloodGlucose":
# CSV Format Time, source, value
event_data = [
record.attrib["startDate"],
record.attrib["sourceName"],
record.attrib["value"],
]
csvWirter.writerow(event_data)
# Insulin
csv_data = open("eksport_insulin.csv", "w", newline="", encoding="utf-8")
csvWirter = csv.writer(csv_data)
root = tree.getroot()
for record in root.iter("Record"):
if record.attrib["type"] == "HKQuantityTypeIdentifierInsulinDelivery":
# CSV Format Time, source, value
event_data = [
record.attrib["startDate"],
record.attrib["sourceName"],
record.attrib["value"],
]
csvWirter.writerow(event_data)
# Carbohydrates
csv_data = open("eksport_carbohydrates.csv", "w", newline="", encoding="utf-8")
csvWirter = csv.writer(csv_data)
root = tree.getroot()
for record in root.iter("Record"):
if record.attrib["type"] == "HKQuantityTypeIdentifierDietaryCarbohydrates":
# CSV Format Time, source, value
event_data = [
record.attrib["startDate"],
record.attrib["sourceName"],
record.attrib["value"],
]
csvWirter.writerow(event_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment