-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrePareData.py
355 lines (320 loc) · 13.4 KB
/
PrePareData.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 2 10:55:54 2020
@author: Administrator
"""
import csv
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.dates as mdate
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib.ticker import FormatStrFormatter
#用reder读取csv文件
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_same_province(str, Province):
province = str[4]
city = str[5]
countryCode = str[2]
#print(str[0])
if province == Province and city == '' and countryCode == 'CN':
# print(province,' ', city)
return True
else:
return False
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_same_country(str, Country):
countrycode = str[2]
provinceCode = str[4]
city = str[5]
#print(str[0])
if countrycode == Country and provinceCode == '' and city == '' :
# print(Country,' ', provinceCode)
return True
else:
return False
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_between_time(str, start):
date2num = mdate.strpdate2num('%Y-%m-%d')
if str[0] == 'date':
return False
else:
datetime2 = date2num(str[0])
#print(datetime2, 'datetime in else -------')
if datetime2 == float(start):
# print(datetime2)
return True
else:
return False
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_not_china(str, Country):
countrycode = str[2]
provinceCode = str[4]
city = str[5]
# dateTime = date2num(str[0])
if countrycode != Country and provinceCode == '' and city == '' :
# if dateTime == float(737394):
# count++
# print(Country,' ', provinceCode)
return True
else:
return False
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_not_Hubei(str, CountryCode):
countrycode = str[2]
province = str[3]
provinceCode = str[4]
city = str[5]
# dateTime = date2num(str[0])
if countrycode == CountryCode and province!= '' and provinceCode != '420000' and city == '' :
# if dateTime == float(737394):
# count++
# print(city,' ', provinceCode)
return True
else:
return False
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_country(str):
province = str[3]
provinceCode = str[4]
city = str[5]
cityCode = str[6]
# dateTime = date2num(str[0])
if province == '' and provinceCode == '' and city == '' and cityCode == '':
# if dateTime == float(737394):
# count++
# print(str[1],' ', str[2])
return True
else:
return False
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def is_china(str, Country):
countryCode = str[2]
provinceCode = str[4]
city = str[5]
# dateTime = date2num(str[0])
if countryCode == Country and provinceCode == '' and city == '' :
# if dateTime == float(737394):
# count++
# print(Country,' ', provinceCode)
return True
else:
return False
def filter_China(filename,resultfile,ProvinceCode):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_same_province(line, ProvinceCode):
#print(line)
writer.writerow(line)
filetowrite.close()
def filter_foreign(filename,resultfile,CountryCode):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_same_country(line, CountryCode):
#print(line)
writer.writerow(line)
filetowrite.close()
def confirm_date(date1,text1):
date2num = mdate.strpdate2num('%Y-%m-%d')
n = 0
while date2num(date1) > float(text1 + n):
n = n + 1
# print('confirm_data function:', n, date2num(date1), float(text1 + n))
return n
#date,country,countryCode,province,provinceCode,city,cityCode,confirmed,suspected,cured,dead
#0 1 2 3 4 5 6 7 8 9 10
def filter_date(filename,resultfile, dateRange):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
date2num = mdate.strpdate2num('%Y-%m-%d')
# date2str= str2date.strftime('%Y-%m-%d')
num1 = 0
num = 0
#dateRange1 = int( date2num(dateRange) - date2num('2019-12-01')); num = num1 = 0
k = 0
sum_temp1 = [0] * 200 # five months
sum_temp2 = [0] * 200
sum_temp3 = [0] * 200
sum_temp4 = [0] * 200
j = 0
# sum_date = []
for line in reader:
# print(line)
list2 = []
if k < 0:
k = k + 1
else:
if k == 0:
num1 = confirm_date(line[0], float(737394))
if is_between_time(line, 737394 + num1):
# print('--------if k==1 between---------', sum_temp1,'buchongshu',num1)
sum_temp1[j] = sum_temp1[j] + int(line[7])
sum_temp2[j] = sum_temp2[j] + int(line[8])
sum_temp3[j] = sum_temp3[j] + int(line[9])
sum_temp4[j] = sum_temp4[j] + int(line[10])
num = num1
k = 10000000000
# print('sum_temp line[7]', j, line[7], sum_temp1[j],'buchongshu',num1, num, k)
else:
num1 = confirm_date(line[0], float(737394))
# print(num1,num,'else fenzhi -=---==-================')
if num < num1 :
# k = 10000000
list2.append(mdate.num2date(737394 + num).strftime('%Y-%m-%d'))
list2.append(sum_temp1[j])
list2.append(sum_temp2[j])
list2.append(sum_temp3[j])
list2.append(sum_temp4[j])
# print(list2)
writer.writerow(list2)
j = j + 1
if is_between_time(line, 737394 + num1):
# print('--------else if between---------', sum_temp1)
sum_temp1[j] = sum_temp1[j] + int(line[7])
sum_temp2[j] = sum_temp2[j] + int(line[8])
sum_temp3[j] = sum_temp3[j] + int(line[9])
sum_temp4[j] = sum_temp4[j] + int(line[10])
# print('sum_temp[j] line[7] j', j, line[7], sum_temp1[j])
num = num1
elif num == num1:
if is_between_time(line, 737394 + num1):
# print('--------else else between---------', sum_temp1)
# sum_temp[j] = sum_temp[j] + int(line[7])
sum_temp1[j] = sum_temp1[j] + int(line[7])
sum_temp2[j] = sum_temp2[j] + int(line[8])
sum_temp3[j] = sum_temp3[j] + int(line[9])
sum_temp4[j] = sum_temp4[j] + int(line[10])
num = num1
# print('sum_temp[j] line[7]', j, line[7], sum_temp1[j])
list2 = []
list2.append(mdate.num2date(737394 + num1).strftime('%Y-%m-%d'))
list2.append(sum_temp1[j])
list2.append(sum_temp2[j])
list2.append(sum_temp3[j])
list2.append(sum_temp4[j])
# print(list2)
writer.writerow(list2)
# sum_foreign.append(sum_temp)
# writer.writerow(sum_foreign)
filetowrite.close()
def get_suspected_num(filename,resultfile):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_country(line):
#print(line)
writer.writerow(line)
filetowrite.close()
def full_of_all(filename,resultfile):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_country(line):
#print(line)
writer.writerow(line)
filetowrite.close()
def full_foreign(filename,resultfile,CountryCode):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_not_china(line, CountryCode):
#print(line)
writer.writerow(line)
filetowrite.close()
def full_china(filename,resultfile,CountryCode):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_china(line, CountryCode):
#print(line)
writer.writerow(line)
filetowrite.close()
def full_without_hubei(filename,resultfile,CountryCode):
reader = csv.reader(open(filename,encoding='utf-8'))
filetowrite = open(resultfile,'w', newline='',encoding='utf-8')
writer = csv.writer(filetowrite)
for line in reader:
if is_not_Hubei(line, CountryCode):
#print(line)
writer.writerow(line)
filetowrite.close()
#def full_of_all(filename,resultfile):
# reader = csv.reader(open(filename))
# filetowrite = open(resultfile,'w', newline='')
# writer = csv.writer(filetowrite)
# for line in reader:
# if is_country(line):
# #print(line)
# writer.writerow(line)
# filetowrite.close()
def get_province(filename,mainFile):
reader = csv.reader(open(filename,encoding='utf-8'))
for line in reader:
dstFile = 'Province/'+line[2]+'.csv'
# print(dstFile)
filter_China(mainFile,dstFile,line[1])
def get_country(filename,mainFile):
readerC = csv.reader(open(filename,encoding='utf-8'))
for line in readerC:
dstFile = 'Country/'+line[2]+'.csv'
# print(dstFile)
filter_foreign(mainFile,dstFile,line[1])
def get_full_foreign(mainFile):
dstFile = 'FullWithoutChina/FullWithoutChina.csv'
full_foreign(mainFile,dstFile,'CN')
def get_full_china(mainFile):
dstFile = 'FullChina/FullChina.csv'
full_china(mainFile,dstFile,'CN')
def get_full_without_Hubei(mainFile):
dstFile = 'FullWithoutHubei/FullWithoutHubei.csv'
full_without_hubei(mainFile,dstFile,'CN')
def get_total(mainFile):
dstFile = 'FULL/FULL.csv'
full_of_all(mainFile,dstFile)
def main_data_prepare(fileNameOfAll, dateTime):
get_full_china(fileNameOfAll)
filter_date('FullChina/FullChina.csv','DateOrder/TimeTestFullChina.csv', dateTime)
get_full_foreign(fileNameOfAll)
filter_date('FullWithoutChina/FullWithoutChina.csv','DateOrder/TimeTestFullForeign.csv', dateTime)
get_full_without_Hubei(fileNameOfAll)
filter_date('FullWithoutHubei/FullWithoutHubei.csv','DateOrder/TimeTestWithoutHubei.csv', dateTime)
get_total(fileNameOfAll)
filter_date('FULL/FULL.csv','DateOrder/TimeTestFULL.csv', dateTime)
get_province('ProvinceCode.csv',fileNameOfAll)
get_country('CountryCode.csv',fileNameOfAll)
if __name__ == '__main__':
fileNameOfAll='Wuhan-2019-nCoV.csv'
# filter_foreign(fileNameOfAll,'Country/The Republic of Namibia.csv','The Republic of Namibia')
dateTime='2020-03-21'
get_full_foreign(fileNameOfAll)
filter_date('FullWithoutChina/FullWithoutChina.csv','DateOrder/TimeTestFullForeign.csv', dateTime)
# filter_China('Wuhan-2019-nCoV.csv','Province/Xizang.csv','540000')
# confirm_date('2020-01-02', float(737394) )
# print('---------------------------------EEEEEEEEEEEEEEEEE------------------------')
# get_full_china('Wuhan-2019-nCoV.csv')
# filter_date('FullChina/FullChina.csv','DateOrder/TimeTestFullChina.csv', '2020-03-03')
# get_full_foreign('Wuhan-2019-nCoV.csv')
# filter_date('FullWithoutChina/FullWithoutChina.csv','DateOrder/TimeTestFullForeign.csv', '2020-03-03')
# get_full_without_Hubei('Wuhan-2019-nCoV.csv')
# filter_date('FullWithoutHubei/FullWithoutHubei.csv','DateOrder/TimeTestWithoutHubei.csv', '2020-03-03')
# get_total('Wuhan-2019-nCoV.csv')
# filter_date('FULL/FULL.csv','DateOrder/TimeTestFULL.csv', '2020-03-03')
#