Skip to content

Instantly share code, notes, and snippets.

@jowlo
Last active April 20, 2024 08:18
Show Gist options
  • Save jowlo/ec3c2c1c78ea3fab4c15f1f6fd3952ac to your computer and use it in GitHub Desktop.
Save jowlo/ec3c2c1c78ea3fab4c15f1f6fd3952ac to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Simple script to transform openscale csv export files to a format accepted by garmin connect at
https://connect.garmin.com/modern/import-data
Note: When importing the language needs to be set to English, otherwise the import fails.
Set everything to metric units and to YYYY-MM-DD date format.
If you want to compute BMI for the file give your height (im meters) as second parameter.
"""
import sys
import csv
from dateutil.parser import parse
from __future__ import print_function
if len(sys.argv) < 2:
print("Missing file to transform\n")
sys.exit(1)
bmi = lambda size: 0;
if len(sys.argv) == 3:
bmi = lambda weight: weight/(float(sys.argv[2])**2)
with open("openScale_garmin_connect_import.csv", "w") as outfile, open(sys.argv[1], "r") as infile:
reader = csv.DictReader(infile, delimiter=",")
writer = csv.writer(outfile, delimiter=",")
outfile.write("Body\n")
outfile.write("Date,Weight,BMI,Fat\n")
for row in reader:
writer.writerow([
parse(row["dateTime"]).strftime('%Y-%m-%d'),
row["weight"],
bmi(float(row["weight"])),
row["fat"]
])
@Ihon75
Copy link

Ihon75 commented Dec 9, 2022

the ( ) around "" is missing

@Ihon75
Copy link

Ihon75 commented Dec 9, 2022

Please change writing mode to "w" only and not "write binary" or line 31 and 32 are not working

@jowlo
Copy link
Author

jowlo commented Dec 9, 2022

Thanks, yeah! I think I wrote this for python 2 because everything on the openscale import/export wiki page wiki was for python 2 only. Should be fine with python2 and python3 now.

@Ihon75
Copy link

Ihon75 commented Dec 9, 2022

It does :-)
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment