-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrun.py
78 lines (68 loc) · 2.06 KB
/
run.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Input: disease_catalog.csv and disease.csv
Output:
AntDesign multilevel selector
[{
"value": "A00-B99",
"label": "某些传染病和寄生虫病",
"children": [{
"value": "A00-A09",
"label": "肠道传染病",
"children": [{
"value": "A00",
"label": "霍乱",
}],
...
}],
...
}]
"""
import csv
import json
cat1_children = {} # {cat1_code: children_list}
cat2_children = {} # {cat2_code: children_list}
res = [] # final array for jsonify
def in_code_range(code, code_range):
start, end = code_range.split('-')
return start <= code <= end
with open('disease_catalog.csv') as file:
reader = csv.DictReader(file, delimiter='\t')
for row in reader:
code = row['code_lower_bound'] + '-' + row['code_upper_bound']
if row['level'] == "1":
_children = []
cat1_children[code] = _children
res.append({
'value': code,
'label': row['catalog'],
'children': _children,
})
continue
cat2_children[code] = []
_children.append({
'value': code,
'label': row['catalog'],
'children': cat2_children[code],
})
with open('disease.csv') as file:
reader = csv.DictReader(file, delimiter='\t')
for d in reader:
for k, v in cat2_children.items():
if in_code_range(d['code'], k):
v.append({
'value': d['code'],
'label': d['disease'],
})
break
else:
# not child of any cat2, add it to matched cat1
for k, v in cat1_children.items():
if in_code_range(d['code'], k):
v.append({
'value': d['code'],
'label': d['disease'],
})
break
with open('diseases.json', 'w', encoding='utf-8') as file:
file.write(u'\uFEFF') # add UTF-8 BOM
json.dump(res, file, ensure_ascii=False, separators=(',', ':'))