把 iPhone 健康資料轉換為 CSV:HealthKit 記錄的資料一覽與 HealthKit Parser 的用法

iPhone 與 Apple Watch 每天都會自動記錄心率、步數、睡眠、血氧等資料。用上幾年,記錄就會達到數百萬筆。這些長期、真實的生活資料很難從別處取得,對研究與開發都很有價值。

不過,「健康」App 匯出的是一個有時超過 1 GB 的巨大 XML 檔案,Excel 等工具無法直接開啟。本文整理了 HealthKit 記錄的資料,並介紹如何用 Mac App HealthKit Parser 把它轉換為 CSV 進行分析。

什麼是 HealthKit

HealthKit 是 Apple 在 iPhone 上集中保存健康資料的框架。除了 iPhone 與 Apple Watch 自動測量的資料,體重計、血壓計的 App 以及記錄睡眠、飲食的 App 等寫入的資料也都匯集在這裡。「健康」App 就是檢視這些資料的 App。

其他 App 要讀取 HealthKit 的資料,需要使用者依資料類型逐一授權。自己開發 App 也可以直接從 HealthKit 讀取資料,但如果只是想把自己的記錄全部取出,使用「健康」App 的匯出功能最簡單。

HealthKit 記錄的資料一覽

HealthKit 的每種資料都有一個識別碼,例如 HKQuantityTypeIdentifierHeartRate(心率)。匯出的資料與 HealthKit Parser 產生的 CSV 的 type 欄中,都會原樣寫入這個識別碼。以下大致依照「健康」App 的分類,整理了主要的資料。

「單位(範例)」是匯出資料中的單位,可能會隨 iPhone 的設定而改變(例如 km 與 mi)。「主要記錄來源」表示資料通常來自哪裡,僅供參考。

活動

資料type 欄中的值單位(範例)主要記錄來源
步數HKQuantityTypeIdentifierStepCountcountiPhone、Apple Watch
步行 + 跑步距離HKQuantityTypeIdentifierDistanceWalkingRunningkmiPhone、Apple Watch
已爬樓層HKQuantityTypeIdentifierFlightsClimbedcountiPhone、Apple Watch
動態能量HKQuantityTypeIdentifierActiveEnergyBurnedkcalApple Watch
靜態能量HKQuantityTypeIdentifierBasalEnergyBurnedkcalApple Watch
運動分鐘數HKQuantityTypeIdentifierAppleExerciseTimeminApple Watch
站立分鐘數HKQuantityTypeIdentifierAppleStandTimeminApple Watch
站立時數HKCategoryTypeIdentifierAppleStandHour—Apple Watch
身體活動強度HKQuantityTypeIdentifierPhysicalEffortkcal/hr·kgApple Watch
心肺適能(最大攝氧量)HKQuantityTypeIdentifierVO2MaxmL/min·kgApple Watch
日光下時間HKQuantityTypeIdentifierTimeInDaylightminApple Watch
騎車距離HKQuantityTypeIdentifierDistanceCyclingkmApple Watch、其他 App 與裝置
游泳距離、划水次數HKQuantityTypeIdentifierDistanceSwimming
HKQuantityTypeIdentifierSwimmingStrokeCount
m, countApple Watch
跑步速度、功率、步幅、垂直振幅、觸地時間HKQuantityTypeIdentifierRunningSpeed
HKQuantityTypeIdentifierRunningPower
HKQuantityTypeIdentifierRunningStrideLength
HKQuantityTypeIdentifierRunningVerticalOscillation
HKQuantityTypeIdentifierRunningGroundContactTime
W, m, cm, ms …Apple Watch
體能訓練強度(估算)HKQuantityTypeIdentifierEstimatedWorkoutEffortScoreappleEffortScoreApple Watch

心臟

資料type 欄中的值單位(範例)主要記錄來源
心率HKQuantityTypeIdentifierHeartRatecount/minApple Watch
靜止心率HKQuantityTypeIdentifierRestingHeartRatecount/minApple Watch
步行平均心率HKQuantityTypeIdentifierWalkingHeartRateAveragecount/minApple Watch
心率變異性(HRV)HKQuantityTypeIdentifierHeartRateVariabilitySDNNmsApple Watch
心率恢復(1 分鐘)HKQuantityTypeIdentifierHeartRateRecoveryOneMinutecount/minApple Watch
心房顫動歷史HKQuantityTypeIdentifierAtrialFibrillationBurden%Apple Watch
高心率、低心率與心律不整通知HKCategoryTypeIdentifierHighHeartRateEvent
HKCategoryTypeIdentifierLowHeartRateEvent
HKCategoryTypeIdentifierIrregularHeartRhythmEvent
—Apple Watch

呼吸

資料type 欄中的值單位(範例)主要記錄來源
呼吸速率HKQuantityTypeIdentifierRespiratoryRatecount/minApple Watch
血氧HKQuantityTypeIdentifierOxygenSaturation%Apple Watch
呼吸干擾HKQuantityTypeIdentifierAppleSleepingBreathingDisturbancescountApple Watch
睡眠呼吸中止通知HKCategoryTypeIdentifierSleepApneaEvent—Apple Watch

睡眠

資料type 欄中的值單位(範例)主要記錄來源
睡眠(臥床、核心、深層、快速動眼期、清醒)HKCategoryTypeIdentifierSleepAnalysis—Apple Watch、iPhone、其他 App 與裝置
睡眠手腕溫度HKQuantityTypeIdentifierAppleSleepingWristTemperaturedegCApple Watch
睡眠時間目標HKDataTypeSleepDurationGoalhr「健康」App

行動能力

資料type 欄中的值單位(範例)主要記錄來源
步行速度HKQuantityTypeIdentifierWalkingSpeedkm/hriPhone
步幅HKQuantityTypeIdentifierWalkingStepLengthcmiPhone
步行不對稱HKQuantityTypeIdentifierWalkingAsymmetryPercentage%iPhone
雙腳支撐時間HKQuantityTypeIdentifierWalkingDoubleSupportPercentage%iPhone
步行穩定度HKQuantityTypeIdentifierAppleWalkingSteadiness%iPhone
樓梯速度(上樓、下樓)HKQuantityTypeIdentifierStairAscentSpeed
HKQuantityTypeIdentifierStairDescentSpeed
m/siPhone、Apple Watch
六分鐘步行距離(估算)HKQuantityTypeIdentifierSixMinuteWalkTestDistancemiPhone

聽力

資料type 欄中的值單位(範例)主要記錄來源
環境音量HKQuantityTypeIdentifierEnvironmentalAudioExposuredBASPLApple Watch
耳機音量HKQuantityTypeIdentifierHeadphoneAudioExposuredBASPLiPhone
嘈雜環境通知HKCategoryTypeIdentifierAudioExposureEvent—Apple Watch

身體測量

資料type 欄中的值單位(範例)主要記錄來源
身高HKQuantityTypeIdentifierHeightcm手動輸入、其他 App 與裝置
體重HKQuantityTypeIdentifierBodyMasskg其他 App 與裝置、手動輸入
身體質量指數(BMI)HKQuantityTypeIdentifierBodyMassIndexcount其他 App 與裝置、手動輸入
體脂率HKQuantityTypeIdentifierBodyFatPercentage%其他 App 與裝置、手動輸入
淨體重HKQuantityTypeIdentifierLeanBodyMasskg其他 App 與裝置、手動輸入
腰圍HKQuantityTypeIdentifierWaistCircumferencecm手動輸入

生命徵象

資料type 欄中的值單位(範例)主要記錄來源
血壓(收縮壓、舒張壓)HKQuantityTypeIdentifierBloodPressureSystolic
HKQuantityTypeIdentifierBloodPressureDiastolic
mmHg其他 App 與裝置、手動輸入
體溫HKQuantityTypeIdentifierBodyTemperaturedegC其他 App 與裝置、手動輸入
基礎體溫HKQuantityTypeIdentifierBasalBodyTemperaturedegC其他 App 與裝置、手動輸入
血糖HKQuantityTypeIdentifierBloodGlucosemg/dL其他 App 與裝置、手動輸入

營養

資料type 欄中的值單位(範例)主要記錄來源
膳食能量、蛋白質、總脂肪、碳水化合物HKQuantityTypeIdentifierDietaryEnergyConsumed
HKQuantityTypeIdentifierDietaryProtein
HKQuantityTypeIdentifierDietaryFatTotal
HKQuantityTypeIdentifierDietaryCarbohydrates
kcal, g其他 App 與裝置、手動輸入
水、咖啡因HKQuantityTypeIdentifierDietaryWater
HKQuantityTypeIdentifierDietaryCaffeine
mL, mg其他 App 與裝置、手動輸入
膳食纖維、維生素、礦物質等(30 多種)HKQuantityTypeIdentifierDietaryFiber
HKQuantityTypeIdentifierDietaryVitaminC
…
g, mg, µg …其他 App 與裝置、手動輸入

其他

資料type 欄中的值單位(範例)主要記錄來源
正念時間HKCategoryTypeIdentifierMindfulSession—Apple Watch、其他 App 與裝置
洗手HKCategoryTypeIdentifierHandwashingEvent—Apple Watch
刷牙HKCategoryTypeIdentifierToothbrushingEvent—其他 App 與裝置
跌倒次數HKQuantityTypeIdentifierNumberOfTimesFallencountApple Watch
週期追蹤(月經等)HKCategoryTypeIdentifierMenstrualFlow
…
—手動輸入、其他 App 與裝置
症狀(頭痛、發燒、咳嗽等)HKCategoryTypeIdentifierHeadache
HKCategoryTypeIdentifierFever
HKCategoryTypeIdentifierCoughing
…
—手動輸入
紫外線指數HKQuantityTypeIdentifierUVExposurecount其他 App 與裝置

單位為「—」的是類別資料,value 欄中是 HKCategoryValueSleepAnalysisAsleepCore(核心睡眠)這樣的類別值。

此外還有聽力測驗結果、體能訓練中的速度等,HealthKit 的資料類型總共超過 100 種。實際包含哪些資料,取決於你使用的裝置與 App。筆者約 9 年(2017–2026 年)的匯出資料中,共有 42 種、約 308 萬筆記錄,其中最多的是 Apple Watch 的動態能量與心率,各約 70 萬筆。

在 iPhone 上匯出健康資料

  1. 在 iPhone 上開啟「健康」App,點一下右上角的個人檔案圖示。
  2. 在頁面最下方點一下「匯出所有健康資料」,出現確認後點一下「匯出」。資料較多時,匯出需要幾分鐘。
  3. 出現分享選單後,用 AirDrop 傳送到 Mac,或儲存到「檔案」後再移到 Mac。
  4. 在 Mac 上按兩下 zip 檔案解壓縮。

解壓縮後會得到一個名為 apple_health_export 的資料夾,裡面包含:

  • export.xml:所有記錄。HealthKit Parser 讀取的就是這個檔案
  • export_cda.xml:以醫療資訊標準格式(HL7 CDA)寫成的記錄
  • electrocardiograms:用 Apple Watch 記錄的心電圖波形(CSV)
  • workout-routes:體能訓練的路線(GPX)

用 HealthKit Parser 轉換為 CSV

HealthKit Parser 是一款讀取 export.xml、把記錄匯出為 CSV 的 Mac App。所有處理都在 Mac 上完成,資料不會被傳送到任何地方。

1. 匯入 export.xml

啟動 HealthKit Parser 後,會開啟一個新文件視窗。按一下工具列中的「Import XML」,選擇解壓縮後資料夾中的 export.xml。

選擇解壓縮後資料夾中的 export.xml
選擇解壓縮後資料夾中的 export.xml

匯入需要一些時間。筆者 1.4 GB 的 export.xml(約 308 萬筆記錄)在 MacBook Pro(M4)上用了約 14 分鐘。匯入完成前,視窗右下角會顯示「Parsing XML…」。

2. 確認資料類型與筆數

匯入完成後,視窗中會顯示記錄的期間(開始與結束)、記錄筆數,以及所有資料類型與各自的筆數。標題與備註可以自由填寫。

記錄的期間、筆數以及每種資料類型的筆數
記錄的期間、筆數以及每種資料類型的筆數

儲存文件後,下次不必重新匯入 export.xml 就能立即開啟。需要依不同期間多次匯出時很方便。

3. 匯出為 CSV

按一下工具列中的「Export CSV」,選擇要匯出的期間與資料類型,然後按一下「Export」。期間可以選「All Time」(全部),也可以用開始與結束日期縮小範圍。

選擇要匯出的期間與資料類型
選擇要匯出的期間與資料類型

也可以一次匯出所有類型,但記錄有數百萬筆時 CSV 會很大。只選擇分析需要的類型,處理起來會輕鬆許多。

CSV 的內容

CSV 中每一列是一筆記錄,包含以下各欄:

欄內容
type資料類型(上表中的識別碼)
unit單位
value值。睡眠等類別資料是表示類別的文字
start, end記錄的開始與結束時間,精確到分鐘,格式取決於 Mac 的「語言與地區」設定
start_UT, end_UT以 Unix 時間(秒)表示的開始與結束時間
creation, creation_UT記錄儲存到 HealthKit 的時間,以及對應的 Unix 時間
device記錄資料的裝置(型號與軟體版本)
source name, source version記錄資料的 App 或裝置的名稱及其版本

以下是匯出的靜止心率範例(僅部分欄):

typeunitvaluestart_UTend_UT
HKQuantityTypeIdentifierRestingHeartRatecount/min631791559483.01791640198.0
HKQuantityTypeIdentifierRestingHeartRatecount/min631791645830.01791714436.0
HKQuantityTypeIdentifierRestingHeartRatecount/min581791737624.01791765440.0

_UT 欄(Unix 時間)

欄名以 _UT 結尾的欄,以 Unix 時間表示日期與時間。Unix 時間是從 1970 年 1 月 1 日 0 時 0 分 0 秒(世界協調時間,UTC)起經過的秒數,不受時區影響。

  • 即使出國旅行或出差改變了時區,記錄的順序與間隔也不會錯亂
  • 只要相減,就能得到睡眠階段等區間的長度(秒)
  • 精確到秒(start 等欄只到分鐘)
  • 便於依時間與研究設備或其他感測器的資料對齊
  • 顯示時可以轉換為任意時區

用 Python(pandas)可以這樣讀取。標題列的逗號後面有空格,因此要加上 skipinitialspace=True。

import pandas as pd

df = pd.read_csv("resting_heart_rate.csv", skipinitialspace=True)

# Unix 時間 → 日期時間(UTC)→ 台灣時間
df["start_local"] = (pd.to_datetime(df["start_UT"], unit="s", utc=True)
                     .dt.tz_convert("Asia/Taipei"))

# 區間長度(秒)
df["seconds"] = df["end_UT"] - df["start_UT"]

在 Excel 中,用 =E2/86400+DATE(1970,1,1) 計算並把儲存格格式設為日期時間,即可得到 UTC 時間(E 欄為 start_UT 時)。要換成台灣時間,再加上 8/24。

分析時的注意事項

  • 同一資料可能重複:同時使用 iPhone 與 Apple Watch 時,步數與距離會被兩者分別記錄。「健康」App 顯示時會去除重複,但匯出的資料中兩者都有。加總前,請用 source name 或 device 欄篩選記錄來源。
  • 百分比是 0 到 1 的值:單位為 % 的資料(血氧、體脂率、步行不對稱等)中,97% 記作 0.97。
  • 確認單位:單位可能隨 iPhone 的設定而改變。計算前請先確認 unit 欄。
  • 類別資料每列是一個區間:睡眠的 value 欄中是 HKCategoryValueSleepAnalysisAsleepCore(核心)、…AsleepDeep(深層)、…AsleepREM(快速動眼期)、…Awake(清醒)等值,每列是一個區間。區間長度為 end_UT − start_UT。
  • 處理他人的資料時:健康資料是非常敏感的個人資訊。在研究中使用參與者的資料時,請遵循所屬機構的倫理審查與知情同意程序。

CSV 中不包含的內容

HealthKit Parser 轉換的是 export.xml 中的各筆記錄。體能訓練的摘要(類型、時間長度、總距離等)、健身圓圈的每日統計,以及出生日期、血型等個人資料不包含在內。體能訓練中的心率、距離與消耗的能量,會作為各筆記錄包含在 CSV 中。

心電圖波形與體能訓練路線,已經以 CSV 與 GPX 檔案的形式放在解壓縮後的資料夾中,可以直接使用。

總結

把 iPhone 與 Apple Watch 的記錄匯出並轉換為 CSV 後,就可以在 Excel、R、Python 等中自由分析。利用 _UT 欄,可以不必擔心時區,把它與其他資料組合起來。

HealthKit Parser 在 Mac App Store 以買斷制販售(macOS 12 或以上版本),沒有訂閱。

→ 查看 HealthKit Parser 的詳細資訊

→ 在 Mac App Store 中查看

更多 HealthKit Parser 相關文章