Skip to content

Instantly share code, notes, and snippets.

@wmp3
Created January 1, 2022 18:13
Show Gist options
  • Save wmp3/3ab9871b53b6215b096f59248d54a47b to your computer and use it in GitHub Desktop.
Save wmp3/3ab9871b53b6215b096f59248d54a47b to your computer and use it in GitHub Desktop.
Process Fitbit weight json data generated from https://www.fitbit.com/settings/data/export
"""Process Fitbit weight json data generated from https://www.fitbit.com/settings/data/export"""
from pathlib import Path
from typing import List
import pandas as pd
BASE_DIR = Path(__file__).parent
DATA_DIR = BASE_DIR.joinpath("fitbit_weight_data")
def get_file_paths(directory_path: Path, file_type: str = "json") -> List[Path]:
return list(directory_path.glob(f"*.{file_type}"))
def main_func():
json_file_paths = [
fp.resolve() for fp in get_file_paths(DATA_DIR, file_type="json")
]
dfs = []
for json_file in json_file_paths:
dfs.append(pd.read_json(json_file))
df = pd.concat(dfs, ignore_index=True, sort=False)
df["timestamp"] = pd.to_datetime(
df["date"].dt.strftime("%Y-%m-%d") + " " + df["time"]
)
df = df[["timestamp", "weight", "bmi"]]
df.sort_values(by="timestamp", ascending=True, inplace=True)
df.to_csv(
"fitbit_weight.csv",
sep="\t",
float_format="%0.1f",
index=False,
encoding="utf-8",
)
if __name__ == "__main__":
main_func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment