Last active
September 19, 2016 15:16
-
-
Save ehoppmann/709e23acbadd3a16d2d4df78c8769225 to your computer and use it in GitHub Desktop.
Convert iOS Weight Recorder / WeightRecord XML file into CSV
This file contains 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 | |
from bs4 import BeautifulSoup | |
import dateparser | |
import csv | |
soup = BeautifulSoup(open('WeightRecorder - 2016-09-18.xml'), "lxml") | |
def kgtolbs(kg): | |
return float(kg) * 2.20462262185 | |
weights = [] | |
for i, entry in enumerate(soup.array.find_all('real')): | |
if (i - 1) % 2 == 0: | |
weights.append(kgtolbs(entry.contents[0])) | |
dates = [] | |
for i, entry in enumerate(soup.array.find_all('date')): | |
dates.append(dateparser.parse(entry.contents[0][0:10])) | |
with open('weight_output.csv', 'w') as f: | |
fieldnames = ['date', 'weight'] | |
writer = csv.DictWriter(f, fieldnames=fieldnames) | |
writer.writeheader() | |
for i in range(len(weights)): | |
writer.writerow({'date': str(dates[i].date()), 'weight': weights[i]}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment