-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep1.py
38 lines (32 loc) · 1.2 KB
/
step1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import xml.etree.ElementTree as ET
import csv
# Define input and output file paths
xml_file = "export.xml" # Replace with your actual XML file name
csv_file = "health_data_records.csv"
# Parse the XML file
tree = ET.parse(xml_file)
root = tree.getroot()
# Open CSV file for writing
with open(csv_file, mode="w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
# Define header row based on attributes in the Record element
header = ["type", "sourceName", "sourceVersion", "unit", "creationDate",
"startDate", "endDate", "value", "device", "metadataEntry"]
writer.writerow(header)
# Extract each Record and write to CSV
for record in root.findall("Record"):
# Extract attributes
row = [
record.get("type"),
record.get("sourceName"),
record.get("sourceVersion"),
record.get("unit"),
record.get("creationDate"),
record.get("startDate"),
record.get("endDate"),
record.get("value"),
record.get("device"),
record.get("metadataEntry")
]
writer.writerow(row)
print(f"Conversion complete! CSV file saved as {csv_file}")