-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_json.py
76 lines (72 loc) · 2.66 KB
/
csv_to_json.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
# Script for decoding of CSV-file with medical cards of patients and recording them to json files
import sys
from mongodb import put
from mongodb import connect
apriory = [
'not mentioned',
'ambiguous',
'diagnosed',
'inconclusive',
'ruled out',
'symptoms present',
'other'
]
formulas = [
'CHF',
'MI'
]
def csv_to_json(mongo):
csv_file_name = 'cci/data/TrainingCases2016-11-16.txt' # CSV-file
#json_directory = 'cci/documents' # Directory for JSON-files and file dir.list with id of cards
print('Decoding of csv file "' + csv_file_name + '".')
for formula in formulas:
try:
f = open(csv_file_name, 'r')
except IOError:
print('File "' + csv_file_name + '" not found.')
sys.exit()
else:
print('Formula: ' + formula)
ids = [] # List of id of cards
chf_ids = {}
for stat in apriory:
chf_ids[stat] = []
for line in f:
card = line.split('\t')
try:
number_of_card = card[0]
ids.append(int(number_of_card))
except ValueError:
continue
else:
print('Card #' + number_of_card)
# Read every card and record to a dictionary
dict = {}
key = ''
h = True
for field in card[2:]:
if field.isspace(): continue
if key == '':
key = field
else:
dict[key] = field
if key.find(formula) != -1:
h = False
for stat in apriory:
if key.find(stat) != -1:
chf_ids[stat].append(number_of_card)
break
key = ''
if h:
print(number_of_card)
# Record dictionary to JSON-file
put("doc.json", dict, number_of_card=number_of_card, mongo=mongo)
# Record of id of cards
f.close()
ids.sort()
#print(str(len(chf_ids))+ ' documents apriory have diagnosis CHF.')
put('results_apriory', chf_ids, formula = formula, mongo=mongo)
if __name__ == '__main__':
mongo = connect()
csv_to_json(mongo)
print('OK.')