Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Import XML file from Apple Health then convert to CSV file
import xml.etree.ElementTree as ET
import csv
import datetime
def convert_xml_to_csv(file_path):
with open(file_path, 'rb') as fd:
root = ET.parse(fd).getroot()
records = []
keys = None
for record in root:
if record.tag == 'Record':
if not keys:
keys = list(record.attrib.keys())
#keys.append('value_c') #for non-numeric value
# convert record.attrib into normal dict to add some features later (from MetaReocrds etc)
att_values = record.attrib
# Get MetadataEntry
for matt in root[2]:
att_values[matt.attrib['key']] = matt.attrib['value']
if matt.attrib['key'] not in keys:
# keys could be different record by record (field "device")
for key in att_values.keys():
if key not in keys:
# final check for special feature 'Value'
except ValueError:
#att_values['value_c'] = att_values['value']
#att_values['value'] = 0
# add to dictionary
print("Attribute records were extracted from {}, found {} entries".format(file_path, len(records)))
# Export file name : Just simply remove extension "xml" then add "csv"
export_file_name = file_path.replace(".xml","%Y%m%d%H%M%S") + ".csv")
with open(export_file_name, 'w') as outfile:
dict_writer = csv.DictWriter(outfile, keys)
print("{} was saved as csv format".format(export_file_name))
if __name__ == '__main__':
convert_xml_to_csv('tmp_files/export.xml') #replace with your file name location

This comment has been minimized.

Copy link

@tim-peterson tim-peterson commented Sep 7, 2020

On my export.xml data I'm getting the following error:

float(att_values['value']) KeyError: 'value'

However, on other people's export.xml data from a similar timeframe is fine.

Any idea why mine is busted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.