-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpcExtractor.py
executable file
·173 lines (134 loc) · 6 KB
/
dpcExtractor.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
167
168
169
170
171
172
173
import dbconfig
import sys
import mysql.connector
# old version of excel
import xlrd
# new version of xlsx
# from openpyxl import load_workbook
# wb = load_workbook('')
# get one SUMMARY sheet with year and database-connection
# commits database, but does not close
# Caution! if dupliated year-nr key appears, it is treated as "seperated cases" and added into existing value.
def convertNaN( value ):
if value == '-':
result = 0
else:
result = float(value)
return result
# Get the sheet type I - with operation ID { 99 | 97 | ... }
def getOneSheetS(sheetname, year, dbcon):
# open the file and prepare for the sheet
wb = xlrd.open_workbook(sheetname)
sheet = wb.sheets()[0]
# prepare the SQL commands
insertdname = "insert into disname (did, dname) values (%s, %s) on duplicate key update dname=%s"
insertcases = "insert into summary (year, nr, did, opid, cases, days) values (?, %s, %s, %s, ?, ?) on duplicate key update cases=cases+?, days=days+?"
cs = dbcon.cursor(prepared=True)
# process the sheet. The data line is starting from row 5 (index 4)
for r in range(4, sheet.nrows):
# the number for the hospital for the year
nr = sheet.cell(r,0).value
# show progress
if r % 20 == 0:
sys.stdout.write( "{0:2.0f}% {1}\r".format( (r-4.0)/(sheet.nrows-4)*100, nr) )
# The data in the line starts from column 4 (index 3)
c = 3
did = "000000"
dname = "------"
occflag = True # flag to distiguish between cases and days
days = 0 # days ( round(occurance * average_days )
occs = {}; # occurance array of cases, indexed by operation_ID
while c < sheet.ncols:
if sheet.cell(0,c).value:
did = sheet.cell(0,c).value
dname = sheet.cell(1,c).value
occflag = True
# SQL insert into summary
# print did, dname
if r == 4:
cs.execute(insertdname , (did, dname, dname))
elif sheet.cell(2,c).value:
occflag = False
ocid = sheet.cell(3,c).value
v = convertNaN(sheet.cell(r,c).value)
if len(ocid) == 2: # ignore 97-subtotal
v = float(v)
if occflag:
occs[ocid] = v;
else:
days = round(occs[ocid] * v)
cs.execute(insertcases, (int(year), nr, did, ocid, occs[ocid], days, occs[ocid], days))
c = c + 1
cs.close()
dbcon.commit()
print("processed ", sheetname)
# Get the sheet type II - with operation/treatment
def getOneSheetT(sheetname, year, dbcon, treatment):
wb = xlrd.open_workbook(sheetname)
sheet = wb.sheets()[0]
insertdname = "insert into disname (did, dname) values (%s, %s) on duplicate key update dname=%s"
if treatment == 1:
insertcases = "insert into tr1 (year, nr, did, withop, withtr1, cases, days)"
else:
insertcases = "insert into tr2 (year, nr, did, withop, withtr2, cases, days)"
insertcases += " values (?, %s, %s, ?, ?, ?, ?) on duplicate key update cases=cases+?, days=days+?"
st = dbcon.cursor(prepared = True)
for r in range(5, sheet.nrows):
nr = sheet.cell(r,0).value
if r % 20 == 0:
sys.stdout.write( "{0:2.0f}% {1}\r".format( (r-4.0)/(sheet.nrows-4)*100, nr) )
c = 3
nr = sheet.cell(r, 0).value
while c < sheet.ncols:
did = sheet.cell(0,c).value
dname = sheet.cell(1,c).value
# for the first line, add disname (update if exists)
if r==5:
st.execute(insertdname, (did, dname, dname))
c_op1tr1 = convertNaN(sheet.cell(r,c ).value)
c_op1tr0 = convertNaN(sheet.cell(r,c+1).value)
c_op0tr1 = convertNaN(sheet.cell(r,c+2).value)
c_op0tr0 = convertNaN(sheet.cell(r,c+3).value)
d_op1tr1 = round( convertNaN(sheet.cell(r,c+4).value) * c_op1tr1 )
d_op1tr0 = round( convertNaN(sheet.cell(r,c+5).value) * c_op1tr0 )
d_op0tr1 = round( convertNaN(sheet.cell(r,c+6).value) * c_op0tr1 )
d_op0tr0 = round( convertNaN(sheet.cell(r,c+7).value) * c_op0tr0 )
st.execute(insertcases , (year, nr, did, True , True , c_op1tr1, d_op1tr1, c_op1tr1, d_op1tr1, ))
st.execute(insertcases , (year, nr, did, True , False, c_op1tr0, d_op1tr0, c_op1tr0, d_op1tr0, ))
st.execute(insertcases , (year, nr, did, False, True , c_op0tr1, d_op0tr1, c_op0tr1, d_op0tr1, ))
st.execute(insertcases , (year, nr, did, False, False, c_op0tr0, d_op0tr0, c_op0tr0, d_op0tr0, ))
c = c + 8
st.close()
dbcon.commit()
print("processed ", sheetname)
# Get the sheet type Hospital List
def getHospitals(sheetname, year, dbcon):
wb = xlrd.open_workbook(sheetname)
sheet = wb.sheets()[0]
sql = "insert into hospitals (year, nr, oldnr, name) values (?, %s, %s, %s)"
sql += " on duplicate key update name = %s"
namecol = 0
for c in range(sheet.ncols):
if sheet.cell(0, c).value=='施設名':
namecol = c
print('施設名 found at ', namecol)
break
else:
print('Cannot find 施設名 column')
sys.exit(-1)
st = dbcon.cursor(prepared=True)
for r in range(1, sheet.nrows):
nr = sheet.cell(r,0).value
if not nr:
break
oldnr = sheet.cell(r,1).value
name = sheet.cell(r,namecol).value
if r % 10 == 0:
sys.stdout.write( "{0:2.0f}% {1}\r".format( (r-1.0)/(sheet.nrows-1)*100, nr) )
try:
st.execute( sql, (year, nr, oldnr, name, name) )
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
st.execute( """update hospitals as new right join hospitals as old on new.oldnr = old.nr and new.year = old.year+1 and new.year = ? set new.id = old.id""", (year,) )
st.close()
dbcon.commit()