-
Notifications
You must be signed in to change notification settings - Fork 0
/
gayatri_brazil.py
170 lines (73 loc) · 2.16 KB
/
gayatri_brazil.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# coding: utf-8
# In[4]:
import os
import pandas as pd
import xmltodict
import json
tctr_trials = []
for file in os.listdir():
if file.endswith('.xml'):
with open(os.path.join(file), encoding = "ISO-8859-1") as xml:
doc = xmltodict.parse(xml.read())
trials_trials=[]
for trial in doc['trials']['trial']:
tctr_trials.append(json.dumps(trial))
# In[5]:
from datetime import date
import csv
def tctr_csv():
with open('tctr1.csv','w', newline = '') as tctr_csv:
writer=csv.writer(tctr_csv)
for val in tctr_trials:
writer.writerow([val])
tctr_csv()
# In[9]:
df= pd.read_csv('tctr1.csv', header= None)
# In[69]:
list(df[0][0].values())[0]
# In[40]:
ff= pd.DataFrame(columns= range(1, 17))
# In[41]:
ff.columns= list((json.loads(df[0][0])).keys())
# In[44]:
ff.columns
# In[49]:
df[0].map(lambda x: type(x))
# In[58]:
for col in ff.columns:
ff[col]= df[0].map(lambda x: x.get(col))
# In[48]:
df[0]= df[0].map(lambda x: json.loads(x))
# In[72]:
pp= ff['trial_identification'][0].keys()
# In[74]:
for i in list(pp):
ff[i]= ff['trial_identification'].map(lambda x: x.get(i))
# In[60]:
ff['trial_identification'].map(lambda x: x.keys())
# In[77]:
ff.drop(range(0, 9), axis= 1, inplace= True)
# In[78]:
ff.columns
# In[87]:
pp= ff['contacts'][0].keys()
for i in list(pp):
ff[i]= ff['contacts'].map(lambda x: x.get(i))
# In[89]:
pp= ff['secondary_ids'][0].keys()
for i in list(pp):
ff[i]= ff['secondary_ids'].map(lambda x: x.get(i))
# In[91]:
pf= ff.drop(['trial_identification', 'sponsors_and_support', 'health_conditions',
'interventions', 'recruitment', 'study', 'outcomes', 'contacts',
'secondary_ids'], axis=1)
# In[93]:
ff.columns
# In[92]:
pf.columns
# In[98]:
pf= pf.applymap(lambda x: str(x)).replace(to_replace= '@', value= '', regex= True).replace(to_replace= "{'value':", value= '', regex= True).replace(to_replace= "{", value= '', regex= True).replace(to_replace= "}", value= '', regex= True)
# In[99]:
pf.to_excel('sk_brazil_terminated_29April2020.xlsx')
# In[ ]: