-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
63 lines (47 loc) · 1.9 KB
/
converter.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import pandas as pd
import re
import json
import os
def md2df(file_path):
"""
Converts the codebook markdown file to a Pandas DataFrame.
**Example**: df = md2df('filename.md')
For now, this function is engineered for the codebook markdown file; however, it can be adjusted to fit other
markdown formats
"""
with open(file_path, 'r') as f:
markdown_text = f.read()
data_dict = {}
current_variable_id = None
current_column = None
human_name = None
variable_id_next = False
for idx, line in enumerate(markdown_text.splitlines()):
if line.startswith("# "):
human_name = line[2:].strip()
elif line.startswith("## Variable ID"):
variable_id_next = True
elif variable_id_next:
current_variable_id = line.strip()
data_dict[current_variable_id] = {"Human Name": human_name}
variable_id_next = False
elif line.startswith("## "):
current_column = line[3:].strip()
elif current_variable_id and current_column:
if current_column in data_dict[current_variable_id]:
data_dict[current_variable_id][current_column] += f"\n{line.strip()}"
else:
data_dict[current_variable_id][current_column] = line.strip()
df = pd.DataFrame(data_dict).transpose()
df.index.name = "Variable ID"
df = df.applymap(lambda x: x.rstrip('\n') if isinstance(x, str) else x)
return df
def df2json(df, file_name, save_path):
"""
Converts a DataFrame to a Json file at the save path
**Example**: df2json(df, file_name= 'file name.json', save_path='desired file save path')
"""
json_data = df.to_json(orient='index')
os.makedirs(save_path, exist_ok=True)
with open(os.path.join(save_path, file_name), 'w') as json_file:
json_file.write(json_data)